Class Container-ship in c++
Needed skills:
- Basic knowledge on class
- Inheritance
- Access Modifiers
Today we will discuss about class container-ship in cpp program. Now the question arises. Why we should focus on class container-ship? I assume that we can access any variables through class and inheritance. But sometimes, using inheritance make your code broad without any needs. You can use container-ship on that purpose.
Now, let's talk about the purpose.
I will give an example and through that I will make you guys understand the purpose of using class container-ship.
Suppose, you are a rich man. And you have a lot of property. You have two cars. one is Ferrari and other is Audi. Now, you want to know the number of wheels and oil method(diesel or patrol)
And you will assign those things according to your name and address.
It's highly recommended to practice this code without solution.
Now, we can do this in two steps.
1. Inheritance
2. Class container-ship
I assume that you guys can be able to code by using inheritance, But we can also do it by using class container-ship. See, Both Audi and Ferrari is a car and can hold the properties of car. So, those two are container. And at last I myself is the owner of those car. It means, I inherit those. Is it seems unclear..?? 😕😕
Okey, don't worry, I will give a syntax here.
#include<iostream>
#include<string>
using namespace std;
class car
{
protected:
string name;
int numberofWheel;
string dieselorpatrol;
public:
void set_name(string name)
{
this->name = name;
}
string get_name()
{
return name;
}
void set_numberofWheel(int numberofWheel)
{
this->numberofWheel = numberofWheel;
}
int get_numberofWheel()
{
return numberofWheel;
}
void set_dieselorpatrol(string dieselorpatrol)
{
this->dieselorpatrol = dieselorpatrol;
}
string get_dieselorpatrol()
{
return dieselorpatrol;
}
};
class Ferarri
{
protected:
string name;
int numberofWheel;
string dieselorpatrol;
public:
car ob1;// Contain car object//
};
class Audi
{
protected:
string name;
int numberofWheel;
string dieselorpatrol;
public:
car ob2;// Contain car object//
};
class Fahim :public Ferarri, public Audi
{
protected:
string name;
string address;
public:
Fahim()
{
cout << "The introduction is: " << endl;
}
Fahim(string name, string address)
{
this->name = name;
this->address = address;
}
void ha_show()
{
cout <<"Owner is: " << name << endl;
cout <<"Owner's address is :" << address << endl;
}
};
int main()
{
Fahim f("Fahim", "Dhaka");
f.ha_show();
f.ob1.set_name("Ferarri");
// f.ob1 or f.ob2 means f is for Fahim and it holds the object of Ferarri and Audi simulaneously//
cout <<"Name: "<< f.ob1.get_name() << endl;// Class container syntex//
f.ob1.set_dieselorpatrol("disel");// Class container syntex//
cout << "Disel or patrol: "<< f.ob1.get_dieselorpatrol()<< endl;// Class container syntex//
f.ob1.set_numberofWheel(4);// Class container syntex//
cout <<"Number of wheel : "<< f.ob1.get_numberofWheel() << endl;// Class container syntex//
f.ob2.set_name("Audi");// Class container syntex//
cout <<"Name : " << f.ob2.get_name() << endl;// Class container syntex//
f.ob2.set_dieselorpatrol("patrol");// Class container syntex//
cout << "Disel or patrol: " << f.ob2.get_dieselorpatrol()<< endl;// Class container syntex//
f.ob2.set_numberofWheel(8);// Class container syntex//
cout << "Number of wheel : " << f.ob2.get_numberofWheel()<< endl;// Class container syntex//
return 0;
}
Happy coding. 😉😉😉😊

Comments
Post a Comment