Solved

Could someone help me initialse the class constructor

Posted on 2013-05-14
4
380 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
[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
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

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…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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