Composition and aggregation in oop(object oriented programming)

 Composition and aggregation

Create a class Distance having a member meter. Now create a friend function named convert() that increments meter by 5 and then converts that meter in kilo meters and return the answer. Call the function in main.  

 #include<iostream>
using namespace std;
class Distance{
            float meter;
            friend void convert(Distance &d1)
            {
                        d1.meter++;
                        d1.meter = d1.meter / 1000;
                        cout << "Meter in killometer is:" << d1.meter << endl;
            }
public:
            Distance(){ meter = 5; }
};
int main()
{
            Distance d1;
            convert(d1);
            getchar();
            getchar();
            return 0;
}      

          

       
 

 

Task 2:

Implement the classes according to the given class diagram. It shows both the composition and aggregation relationships.
Driver has a Vehicle and Engine is a part of that Vehicle.

[Note: consider the necessary data members, constructors, destructors and member functions accordingly]

#include<iostream>
using namespace std;
class Vihicle{
            int no;
public:
            Vihicle(){ no = 0; }
            Vihicle(int a){
                        no = a;
                        cout << "No is set in vihicle" << endl;
            }
            int getno()
            {
                        return no;
            }
            void data(int a)
            {
                        no = a;
                        cout << "Values set" << endl;
            }
            void show()
            {
                        cout << "Value of a is:" << getno() << endl;
            }
 
};
class Driver{
            Vihicle *p;
public:
            void setp(Vihicle *a)
            {
                        p = a;
                        cout << "function call successfully in Driver" << endl;
                        cout << "I updated the value here" << endl;
                        p->data(10);
            }
            void show(Vihicle a1)
            {
                        cout << "Vihicle show called" << endl;
                        a1.show();
            }
};
class Engin{
            Vihicle *v1, *v2;
public:
            Engin(){
                        cout << "Engin Construct" << endl;
                        v1 = new Vihicle;
                        v2 = new Vihicle;
                        v1->data(100); v2->data(12);
            };
            void show()
            {
                        v1->show(); cout << endl;
                        v2->show(); cout << endl;
                        cout << "Show called" << endl;
            }
            ~Engin()
            {
                        delete v1;
                        delete v2;
                        cout << "Destruct done" << endl;
            }
};
int main()
{
            Vihicle v(12); Driver a; Engin e1;
            a.setp(&v); cout << endl;
            a.show(v); cout << endl; e1.show(); cout << endl;
            e1.~Engin();
            getchar();
            getchar();
            return 0;
}       

          

       
 

Comments

Popular posts from this blog

Multiple inheritance,friend function and multiple file in oop(object oriented programming)

Concepts of OOP (object oriented programming)

Concepts in c++........