Solved

Could someone help me initialse the class constructor

Posted on 2013-05-14
4
356 Views
Last Modified: 2013-05-19
ClassA{
public:
        ClassA(int kg);
      virtual ~ClassA();
private:
      int kk;
};
ClassA::ClassA(int kg) {
      kk = kg;
}

ClassA::~ClassA() {
}
class classB {
public:
      classB();
      virtual ~classB();  
        ClassA ca;
};

classB::classB()//I am getting error at this line
{
     //how to initialse the constructor of B
}

classB::~classB() {

}
0
Comment
Question by:pvinodp
4 Comments
 
LVL 30

Assisted Solution

by:Zoppo
Zoppo earned 300 total points
ID: 39164724
Hi pvinodp,

the problem is the constructor of ClassA expects an int as argument, but the way it's implemented it can only be used when there's a default constructor. You can either add a default constructor to ClassA or change the existing constructorto take a default argument or initialize 'ca' in classB's constructor using an initialization list, i.e.:
// method 1: implement a default constructor
class ClassA
    ClassA(int kg=0); // added default argument so this becomes a default constructor
    virtual ~ClassA();
private:
      int kk;
};

ClassA::ClassA(int kg) {
      kk = kg;
}

// method 2: initialize 'ca'
classB::classB()
: ca( 5 ) // using 5 for just as sample
{
}

Open in new window

Hope this helps,

ZOPPO
0
 
LVL 32

Accepted Solution

by:
phoffric earned 200 total points
ID: 39165743
Sometimes you are not able to modify ClassA as it may be written by a third party. But if you can modify classB, then you could take a couple of approaches. One approach is to provide a default value for classA in the classB constructor initialization list (as Zoppo as shown). Another approach is to add another classB constructor that takes an arg that can be used for the ca initialization. Both approaches are shown below with no functional change to ClassA.
#include <iostream>
using namespace std;

class ClassA {
public:
      ClassA(int kg) : kk(kg) {}
      virtual ~ClassA() {}
      int get_kk() {return kk;}
private:
      int kk;
};


class classB {
public:
      classB()  : ca(12) {}
      classB(int Bkg) : ca(Bkg) {} // add constructor to provide value for ClassA
      virtual ~classB() {}  
      ClassA ca;
};


int main() {
   ClassA myA(10);
   classB myClassB;
   classB myClassB2(22);

   cout << myA.get_kk() << endl;
   cout << myClassB.ca.get_kk() << endl;
   cout << myClassB2.ca.get_kk() << endl;
}

Open in new window

0
 

Author Closing Comment

by:pvinodp
ID: 39167169
thanks for your answers
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

746 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

9 Experts available now in Live!

Get 1:1 Help Now