Solved

C++ syntax: class inheritance, constructor

Posted on 2013-12-28
3
500 Views
Last Modified: 2013-12-28
I have a base class A, a class B which inherits from A, and I want a constructor for class B which can take an instance of class A.

#include <string>

class A
{
public:
  A(int _i, std::string _ss);
private:
  int i;
  std::string ss;
public:
  int GetI(void) { return i; }
  void SetI(int _i) { i = _i; }
  std::string Getss(void) { return ss; }
  void Setss(std::string _ss;) { ss = _ss; }
}

class B : public A
{
public:
  B(A a, float f);   //???
private:
  float f;
public:
  void Setf(float _f) { f = _f; }
  float Getf(void);
}

B::B(A a, float f)
{
???
}

Open in new window

0
Comment
Question by:deleyd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 39743725
And your question is?
0
 
LVL 32

Accepted Solution

by:
phoffric earned 250 total points
ID: 39743952
>>   B(A a, float f);   //???

B is constructed by constructing A (i.e., B's base class) and optionally along with doing whatever construction desired for B. You can use the A's default copy constructor for B's constructor. Here is one likely possibility:

  B(A a, float ff) : A(a), f(ff) {};

In this case, a's data members will be copied into B's base class data members of A.

I changed B's constructor's 2nd arg to ff not because I had to but because, in general, I just don't happen to like using the same name for different entities.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 39743957
In case you would like to read up on default copy constructors, here are some links:
   http://en.cppreference.com/w/cpp/language/copy_constructor
   http://www.cplusplus.com/articles/y8hv0pDG/
   http://en.wikipedia.org/wiki/Copy_constructor
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Create a path if not exists 7 119
gdb doesn't stop on breakpoint 2 132
Precision Problem in C++ 7 64
Autosar OS Multicore Share Resources confusion ? 2 185
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question