+ 2

An example of composition in c++ with 3 classes

Someone can make a example of composition in c++ with 3 classes, like Birthday and Person, thank.

7th Mar 2017, 7:36 PM
Dawid Pado
Dawid Pado - avatar
10 Réponses
+ 1
well you could define your two primary classes like they show in the lessons then generate parent by inheriting from person. Trying to get you to peek into the lessons for the answer but I can write more if you're unable to find it.
7th Mar 2017, 8:04 PM
Richard Valles
Richard Valles - avatar
+ 1
#include <iostream> using namespace std; class BaseClass1 { public: BaseClass1() { cout << "BaseClass1 constructor." << endl; } }; class BaseClass2 { public: BaseClass2() { cout << "BaseClass2 constructor." << endl; } }; class BaseClass3{ public: BaseClass3() { cout << "BaseClass3 constructor." << endl; } }; class DerivedClass : public BaseClass1, public BaseClass2, public BaseClass3 { public: DerivedClass() { cout << "DerivedClass constructor." << endl; } }; int main() { DerivedClass dc; }
8th Mar 2017, 5:50 PM
Dawid Pado
Dawid Pado - avatar
+ 1
I I found this but it is not similar to the example given in the lessons
8th Mar 2017, 5:53 PM
Dawid Pado
Dawid Pado - avatar
0
Do you mean create a class that incorporates multiple other classes? like Parent from Person that has a Birthday?
7th Mar 2017, 7:39 PM
Richard Valles
Richard Valles - avatar
0
yes 3 or more
7th Mar 2017, 7:40 PM
Dawid Pado
Dawid Pado - avatar
0
write more please
8th Mar 2017, 1:04 PM
Dawid Pado
Dawid Pado - avatar
0
gotta show me you tried first :). What did you find in the examples so far?
8th Mar 2017, 5:32 PM
Richard Valles
Richard Valles - avatar
0
Well what you have is right however there are no defined functions in any of the other classes so there is nothing being inherited. What is it you're trying to do in particular?
8th Mar 2017, 6:31 PM
Richard Valles
Richard Valles - avatar
14th Mar 2017, 4:10 PM
Dawid Pado
Dawid Pado - avatar
0
is this is right??? #include<iostream> using namespace std; class website{ char *name; public: website(){ cout<<"website default constructor called\n"; } website(char *name) { cout<<"website parameterized constructor called\n";} }; class webpage{ double width,height; public: webpage(){ cout<<"WebPage default constructor Called\n"; } webpage(double width, double height){ cout<<"webpage parameterized constructor called!\n"; } }; class links{ char *name; public: links(){ cout<<"Links default constructor called\n"; } links(char *name) { cout<<"Links parameterized constructor called\n"; } }; main() { website web1; webpage page1; links link1; website webnew("Google"); webpage page2(640,480); links link2("Home"); return 0; system("pause"); }
23rd Nov 2017, 11:31 AM
SHAHID MEHMOOD
SHAHID MEHMOOD - avatar