Solved

C++ syntax: class inheritance, constructor

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

688 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