Showing posts with label concept of OOP. Show all posts
Showing posts with label concept of OOP. Show all posts

Wednesday, 18 July 2012

OOP: Composition in C++

#include<iostream>
#include<string>

using namespace std;


class employee
{
private: string employee_name;
public: employee(string name)
        {
            employee_name=name;
        }
public: string get_employee_name()
        {
            return employee_name;
        }
public: void show_employee_name()
        {
            cout<<"The name of the employee of this organization is == "<<employee_name<<endl;
        }

};
class organization
{
private: string organization_name;
         employee *myemployee;
public: organization(string name_of_employee, string name_of_organization)
        {
            myemployee=new employee(name_of_employee);
            organization_name=name_of_organization;
        }
public: void show()
        {
            cout<<"The organization name is == "<<organization_name<<endl;
            myemployee->show_employee_name();
        }
};
int main()
{
    string org_name,employ_name;
    cout<<"This is the organization structure"<<endl;
    cout<<"Enter the organization name == ";
    getline(cin,org_name);
    cout<<"Enter the name of employee == "<<endl;
    getline(cin,employ_name);
    organization myorganization(employ_name,org_name);
    myorganization.show();



    getchar();
    getchar();
    return 0;
}

Saturday, 14 July 2012

OOP : Aggregation in C++

#include<string>
#include<iostream>

using namespace std;

class Mobile
{
public: string IMEI_NO;
public: string Model;
public: Mobile(string im_no,string model)
        {
            IMEI_NO=im_no;
            Model=model;
        }
public: void show()
        {
            cout<<"IMEI no is == "<<IMEI_NO<<endl;
            cout<<"Model of your mobile is == "<<Model<<endl;
        }
};


class Person
{
    Mobile *mymobile;
public: string name;
public: string CNIC;
public: Person(string n , string c)
        {
            name=n;
            CNIC=c;
        }
public: void show()
        {
            cout<<"The name of the person is == "<<name<<endl;
            cout<<"The CNIC of a particular person is == "<<CNIC<<endl;
            mymobile->show();       
        }
public:void setmobile(Mobile * m1)
        {
            mymobile=m1;
        }
};

int main()
{
    Mobile *mob;
    mob=new Mobile("12589874458580","nokia 3310");
    Person myperson("Anonymous","37405-58925986-5");

    myperson.setmobile(mob);
    myperson.show();

    getchar();
    return 0;
}

Wednesday, 11 July 2012

Composition in C++


#include<iostream>
#include<string>
using namespace std;

class author_name
{
private: string author;
public: author_name(string auth)
{
author=auth;
}
public: string get_author()
{
return author;
}
public: void show()
{
cout<<"Author Name is == "<<author<<endl;
}
};



class Book
{
private: string book_name;
author_name *myauthor;
public: Book(string aut,string b_name)
{
myauthor=new author_name(aut);
book_name=b_name;
}
public: void show()
{
cout<<"Book name == "<<book_name<<endl;
myauthor->show();
}
};

int main()
{
Book mybook("ABCDEF","C++ LANGUAGE");
mybook.show();
getchar();
return 0;
}

OOP: Composition in C++

#include<iostream>
#include<string>
using namespace std;

class engine
{
private: int capacity;

public: engine(int c)
        {
            capacity=c;
        }

public: int get_engine()
        {
            return capacity;
        }

public: void show()
        {
            cout<<"Capacity of The engine is == "<<capacity<<endl;
        }
};



class car
{
private: string car_name;

         engine *myengine;

public: car(int cap, string name)
        {
            myengine= new engine(cap);
            car_name=name;
        }

public: void show()
        {
            cout<<"Company name == "<<car_name<<endl;
            myengine->show();
        }
};



int main()
{
    string car_name;
    int capacity_of_car;

    cout<<"Enter the Name of car == "<<endl;
    getline(cin,car_name);

    cout<<"Enter The Capacity of the car == ";
    cin>>capacity_of_car;

    car mycar(capacity_of_car,car_name);

    mycar.show();

    getchar();
    getchar();
    return 0;
}