+ 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.
10 Answers
+ 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.
+ 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; }
+ 1
I I found this but it is not similar to the example given in the lessons
0
Do you mean create a class that incorporates multiple other classes? like Parent from Person that has a Birthday?
0
yes
3 or more
0
write more please
0
gotta show me you tried first :). What did you find in the examples so far?
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?
0
I understand how to do it https://code.sololearn.com/cL9X142bNcBu/?ref=app
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");
}