?
Solved

Could someone help me initialse the class constructor

Posted on 2013-05-14
4
Medium Priority
?
388 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 1200 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 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

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