Solved

Could someone help me initialse the class constructor

Posted on 2013-05-14
4
368 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 31

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

820 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