Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to create an object of one class in another class?

Status
Not open for further replies.

milan.rajik

Banned
Advanced Member level 5
Joined
Apr 1, 2013
Messages
2,524
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Visit site
Activity points
0
How to create an object of one class in another class? I am trying with both Visual Studio 2010 and NetBeans 1.7.3 IDE + MinGW Tool set.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Greet{
    
        public:
            
            Greet(string name){         //Constructor
                msg2 = name;
            }
            
            void SayHello(){
                
                cout << msg2 << endl;
            }
    
            string msg2;
 
        private:
            
            string msg;           //This is data member declaration
    
};
 
 
 
 
class Greetings{
    
        Greet myGreet1("Hi from Greet1");  //This line gives error
        
        public:           
            
            Greetings(string name){     //Constructor
                
                msg = name;
            }
            
            void SayHello(){
                
                cout << "From Object of class Greetings" << endl;                
                
            } 
    
        private:
            
            string msg;
    
};

 

Hello,

Maybe this thread is a bit old, but if you are still interested, there is a problem with line 27. The:
Code:
Greet myGreet1("Hi from Greet1");
line cannot be in the class definition. What is happening is that you are trying to initialize a non-static member variable (myGreet1) in the class definition. This should instead occur in the constructor. You would probably want something like this:
Code:
class Greetings{
    
        
        public:           
            
            Greetings(string name)    // constructor
              : myGreet1("Hi from Greet1") {     //Constructor initializer list
                    
                msg = name;
            }
            
            void SayHello(){
                
                cout << "From Object of class Greetings" << endl;                
                
            } 
    
        private:
            
            string msg;
            Greet myGreet1;  // note that you declare the inner greet here
    
};

Now this will compile and run, but it won't print "Hi from Greet1", because your Greetings class doesn't tell the greet class to do anything other than run his constructor. You probably want to do something like the following:

Code:
class Greetings{
    
        
        public:           
            
            Greetings(string name)    // constructor
              : myGreet1("Hi from Greet1") {     //Constructor initializer list
                    
                msg = name;
            }
            
            void SayHello(){

                myGreet1.SayHello(); // tell the inner greet to do something

                cout << "From Object of class Greetings" << endl;                
                
            } 
    
        private:
            
            string msg;
            Greet myGreet1;  // note that you declare the inner greet here
    
};

Anyways, let me know if you are still waiting on this and if this makes sense. Good Luck!
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top