Solved

C++ syntax: class inheritance, constructor

Posted on 2013-12-28
3
486 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
Comment Utility
And your question is?
0
 
LVL 32

Accepted Solution

by:
phoffric earned 250 total points
Comment Utility
>>   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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

771 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

11 Experts available now in Live!

Get 1:1 Help Now