Not getting required output composition relation between classes

I want to display the following message:

**WebPage default constructor called**
**Links default constructor called**


**WebPage default constructor called**
**Links default constructor called**


**WebPage default constructor called**
**Links default constructor called**



**WebPage parameterized constructor called !**
**Links parameterized constructor called !**
**WebSite parameterized constructor called !**



but my code output is



**WebPage default constructor called**
**Links default constructor called**


**WebPage default constructor called**
**Links default constructor called**


**WebPage parameterized called !**
**Links parameterized called !**



**WebPage default constructor called**
**Links default constructor called**
**WebSite parameterized constructor called !**


In short: First 6 lines should print default constructors of webpage and links only while last 3 lines should print parametrized contructor

There is a difference between last 5 lines. I don't know why these are not as expected? Can someone point out at my mistake?

P.S: There is a composition relation between class Webpage, links and website. As webpage and class are composed of website. Here is my code:

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 #include<iostream>
    using namespace std;
    
    class links
    {	
        private:
        const char *name;	
        public:
        links()
        {  
             cout<<"\nLinks default constructor called";  
        }
        links(const char *page)
        {
            cout<<"\nLinks parameterized constructor called!";	
    	
        }
    };
    
    class webpage
    {
        private:
            double width;
            double height;	
    
        public:
            webpage()
            { 
                cout<<"\n\nWebpage default constructor called";
            }	
    
            webpage(double height,double width)
            {
                cout<<"\n\nWebpage parameterized constructor called!";
            }		
    };
    
    class website:public webpage,public links
    {	
        private:
        char *name1;
        webpage a;
        links b;
        
        public:
        website() 
        {
            
        }
        website(const char *site):webpage(0.5,9.0),links("page")
        {
            cout<<"\nWebsite parameterized constructor called!";
        }	
    };
    	
    int main()
    {
    	website a;
    	website b("anything");
    	
    
    	return 0;	
    }
 
    


 
Last edited on
Lines 38,42,43: You have both inheritance and composition of the same object types. Why?

Line 50: webpage's and links' parameterized constructors are going to be called. In addition, lines 42 and 43 are going to call webpage's and links' default constructors.
I am a little bit confused how to correcti it. Can you correct this solution ?
I'm not sure what you're trying to do.

Your title suggests you want to use composition. In that case, I would remove the inheritance from line 38 and change line 50 as follows:
 
website (const char *site) : a(0.5,9.0), b("page")


Using the IS A (inheritance) and HAS A (composition) tests suggests composition is probably the correct choice. A website HAS A webpage and a website HAS A links (although the plural here is confusing).



Constructing a
Webpage default constructor called
Links default constructor called
Website default constructor called!

Constructing b
Webpage parameterized constructor called!
Links parameterized constructor called!
Website parameterized constructor called!
Press any key to continue . . .
Last edited on
Topic archived. No new replies allowed.