Skip to main content

Class Containership

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

Popular posts from this blog

Remote Dictionary Server

  Today I want to discuss about REDIS Cache. In a simple way, it’s like making web apps more effective. What they say ? From Redis  official website , “Redis is an open-source, in-memory data structure store, used as a database, cache and message broker. It supports data structures . I t can be thought as a NOSql DB which store data as a key value pair in the system memory. Now, let’s talk about caching. Difference Between MongoDB And Redis MongoDB is a disk based database optimized for operational simplicity. It has a huge data volumes. And Redis is an in-memory, persistent  data Structure store that is popular for performing operations with maximum effectiveness. Caching Caching is a process where the data are stored in caches so that those can be retrieved faster. It is for making user experience better. Redis Cache As Redis is an in-memory data structure store  where data access operations are performed faster. That’s why it’s the perfect method for caching. The ...

Some Important learning sites for Dynamic programming

Now, We will discuss some of the most useful sites for learning Dynamic Programming. Here, I have mentioned some of those . :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: medium.freecodecamp.org -  Demystifying Dynamic Programming iarcs.org.in -  Dynamic Programming - Tiling topcoder.com -  Dynamic Programming – From Novice to Advanced illinois.edu -  Dynamic Programming  ;(Exercises are recommended) codechef.com -  Dynamic Programming geeksforgeeks.org -  Dynamic Programming  ;(Contains a lot of practice sessions) MIT OCW (Contains some Advanced topics as well) Dynamic Programming I Dynamic Programming II Dynamic Programming III Dynamic Programming IV Happy Learning...Thanks 😉😉😊😊  

Params in C#

C# HAS A FEATURE. IT CAN SPECIFY A METHOD PARAMETER WHICH WILL TAKE A VARIABLE NUMBER OF  ARGUMENTS. WE USED TO DO THOSE THINGS BY USING POINTER IN C++ C# MAKES OUR LIFE EASIER. ALL WE NEED IS A KEYWORD. AND THAT IS "PARAMS" LET'S HAVE THE FUN REMEMBER, THE PARAMS PARAMETER MUST BE A SINGLE ARRAY.   public class MyClass         {                          public static void amarmethod(params int[] amararrayerkichuelements)             {                                 for (int i = 0; i < amararrayerkichuelements.Length; i++)                 {                     Console.Write(amararrayerkichuelements[i] + " ");                 } ...