?
Solved

C++ syntax: class inheritance, constructor

Posted on 2013-12-28
3
Medium Priority
?
506 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 1000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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.
Suggested Courses

762 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