Solved

C++ syntax: class inheritance, constructor

Posted on 2013-12-28
3
489 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now