You are on page 1of 16

ASSIGNMENT

OBJECT ORIENTED
PROGRAMMING
C++

Submitted To : Sir. Zahid Aslam


Submitted By : Majid Shahid
Roll No : 653
st
Class : MCS 1
DEFAULT COPY CONSTRUCTOR

EXAMPLE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class book
{
public :
int bookid;
char title[30];
char author[30];
int pages;
void input()
{
cout<<"Enter a book ID: ";
cin>>bookid;
cout<<"Enter title of book: ";
gets(title);
cout<<"Enter author name: ";
gets(author);
cout<<"Enter no of pages: ";
cin>>pages;
}
void show()
{
cout<<"Book id = "<<bookid<<endl;
cout<<"Book title = "<<title<<endl;
cout<<"Author name= "<<author<<endl;
cout<<"No of pages= "<<pages;
}
};
void main()
{
clrscr();
book s;
s.input();
book b(s);
cout<<"Object s values are...."<<endl;
s.show();
cout<<"\n object b values are..."<<endl;
b.show();
getch();
}

EXAMPLE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class car
{
public :
int model;
char name[30];
char cc[30];
char color[20];
void input()
{
cout<<"Enter car model year: ";
cin>>model;
cout<<"Enter company name: ";
gets(name);
cout<<"Enter average cc: ";
gets(cc);
cout<<"Enter color: ";
gets(color);
}
void show()
{
cout<<"Car Model = "<<model<<endl;
cout<<"Car manufacture = "<<name<<endl;
cout<<"Car CC = "<<cc<<endl;
cout<<"Car color = "<<color;
}
};
void main()
{
clrscr();
car s;
s.input();
car b(s);
cout<<"object s values are..."<<endl;
s.show();
cout<<"\nobject b values are..."<<endl;
b.show();
getch();
}

EXAMPLE

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
public :
int rno;
char name[30];
float cgpa;
int age;
void input()
{
cout<<"Enter a student name: ";
gets(name);
cout<<"Enter roll no: ";
cin>>rno;
cout<<"Enter its age: ";
cin>>age;
cout<<"Enter CGPA: ";
cin>>cgpa;
}
void show()
{
cout<<"Student name = "<<name<<endl;
cout<<"Roll Num ="<<rno<<endl;
cout<<"Student age= "<<age<<" Years"<<endl;
cout<<"CGPA = "<<cgpa;
}
};
void main()
{
clrscr();
student s;
s.input();
student b(s);
cout<<"Object s values are..."<<endl;
s.show();
cout<<"\n object b values are.."<<endl;
b.show();
getch();
}

EXAMPLE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class teacher
{
public:
int tid;
char name[30];
char subject[30];
void input()
{
cout<<"Enter Teacher id ID: ";
cin>>tid;
cout<<"Enter name of teacher: ";
gets(name);
cout<<"Enter Subject name: ";
gets(subject);
}
void show()
{
cout<<"Teacherid = "<<tid<<endl;
cout<<"Teacher name = "<<name<<endl;
cout<<"Subject = "<<subject<<endl;
}
};
void main()
{
clrscr();
teacher s;
s.input();
teacher a(s);
cout<<"First object values"<<endl;
s.show();
cout<<"after default copy constructor"<<endl;
a.show();
getch();
}

EXAMPLE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person
{
public :
char name[30];
int age;
char mob[20];
void fun()
{
cout<<"Enter a person name: ";
gets(name);
cout<<"Enter its age: ";
cin>>age;
cout<<"Enter contact number: ";
gets(mob);
}
void show()
{

cout<<"Person name= "<<name<<endl;


cout<<"Person age= "<<age<<" Years"<<endl;
cout<<"Person contact # = "<<mob;
}
};
void main()
{
clrscr();
person a;
a.fun();
person b(a);
cout<<"Object a values are.."<<endl;
a.show();
cout<<endl<<"Object B values are.."<<endl;
b.show();
getch();
}

DESTRUCTORS
EXAMPLE

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
class car
{
public :
int model;
char name[30];
char cc[30];
char color[20];
car()
{
cout<<"Enter car model year: ";
cin>>model;
cout<<"Enter company name: ";
gets(name);
cout<<"Enter average cc: ";
gets(cc);
cout<<"Enter color: ";
gets(color);
}
~car()
{
ofstream file("abc.txt");
file<<model<<endl<<name<<endl<<cc<<endl<<color<<endl;
}
};
void main()
{
clrscr();
car s;
getch();
}

EXAMPLE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class test
{
public :
int n;
test()
{
cout<<"object created Successfully. ";
}
~test()
{
cout<<"object destroyed .";
}
};
void main()
{
clrscr();
test a,b;
getch();
}

You might also like