You are on page 1of 24

OBJECT ORIENTED PROGRAMMING LAB

M.C.A 600253

PROGRAMS LIST

1. ENUMERATION WITH FUNCTION OVERLOADING


2. SCOPE AND STORAGE CLASS
3. ABSTRACT DATA TYPE – STACK AND QUEUE
4. CONSTRUCTORS AND DESTRUCTORS AND CONSTRUCTOR OVERLOADING
5. STATIC MEMBERS AND STATIC METHODS
6. BIT FIELDS
7. BINARY OPERATOR OVERLOADING
8. UNARY OPERATOR OVERLOADING IN POSTFIX AND PREFIX
9. ITERATORS AND CONTAINERS
10. FUNCTION TEMPLATES
11. CLASS TEMPLATES
12. VARIOUS FORMS OF INHERITANCE
13. VIRTUAL FUNCTIONS
14. EXCEPTION HANDLING
1. ENUMERATION AND FUNCTION OVERLOADING
Program

#include<iostream.h>
#include<conio.h>

#define PI 3.14

class calcarea {
public:
float area(float r) {
return (PI * (r * r));
}

float area(float l,float w) {


return (l * w);
}
};

void main() {
int ch;
calcarea c;
enum mnu
{
circle = 1,
rectangle = 2,
exit = 3
}m;

clrscr();
cout<<"Select one shape to calculate it's area..\n";
cout<<"1. CIRCLE\t2. RECTANGLE\t3. EXIT\n";
cout<<"Your choice : ";
cin>>ch;

m = (int)ch;

switch(m) {
case rectangle:
float l,w;
cout<<"Enter the length and width of rectangle :\n";
cin>>l>>w;
cout<<"Area of rectangle : "<<c.area(l,w);
getch();
main();
break;
case circle:
float r;
cout<<"Enter the radius of cirlce :\n";
cin>>r;
cout<<"Area of circle : "<<c.area(r);
getch();
main();
break;
case exit:
break;
default:
cout<<"Invalid choice. Try again..!";
getch();
main();
break;
}
}

2. SCOPE AND STORAGE CLASS - SCOPE


Program

#include<iostream.h>
#include<conio.h>

int g;
class scope
{
private:
int g;
public:
void fun()
{
int g = 5;
cout<<"\nLocal variable 'g' : "<<g;
}

void fun2()
{
g = 10;
cout<<"\nClass variable 'g' : "<<g;
}
};
void main()
{
clrscr();
scope s;
s.fun();
s.fun2();
g=25;
cout<<"\nGlobal variable 'g' : "<<g;
getch();
}

2. SCOPE AND STORAGE CLASS – STORAGE CLASS


Program

#include<iostream.h>
#include<conio.h>
#include<string.h>

int i;
class storage
{
public:
void registerclass()
{
register int i;
char str[20];
cout<<"\nEnter a word to reverse : ";
cin>>str;
cout<<"\nThe reserved string is\n";
for(i=strlen(str);i>=0;i--)
cout<<str[i];
cout<<"\n";
}

void autoclass()
{
auto int i = 1;
{
auto int i = 2;
{
auto int i = 3;
cout<<"\nI : "<<i;
}
cout<<"\nI : "<<i;
}
cout<<"\nI : "<<i;
}
void staticclass()
{
static int i = 5;
cout<<"\nStatic variable 'i' : "<<i++;
}
};

void main()
{
clrscr();
storage s;
s.registerclass();
i = 50;
cout<<"\nExtern variable 'i' : "<<i;
s.staticclass();
s.staticclass();
s.autoclass();
getch();
}

3. ABSTRACT DATA TYPE - STACK


Program

/* C++ program for illustrating


ADT such as Stack and Queue */

#include<iostream.h>
#include<conio.h>

class stack {
public:
int item[100];
int top;
stack() {
top = -1;
}

public:
void push() {
if(top < 100) {
top = top + 1;
cin>>item[top];
cout<<"Item pushed : "<<item[top];
cout<<"\nTop = "<<top;
}
else
cout<<"Not enough space. Stack Overloaded.\n";
getch();
}

void pop() {
if (top < 0)
cout<<"Stack is empty..!";
else {
top--;
cout<<"Element popped out : "<<item[top];
cout<<"\nTop = "<<top;
}
getch();
}

void display() {
int i;
if(top<0)
cout<<"Stack is empty..";
else {
cout<<"Elements in stack!\n";
for(i=0;i<top;i++)
cout<<item[top];
}
}
};

class queue {

};

void main()
{
clrscr();
stack s;
int ch;
cout<<"\nStack.................\n";
cout<<"1. Push\t2. Pop\t3. Display\t4. Exit\n";
cout<<"Your choice:";
cin>>ch;
switch(ch)
{
case 1:
s.push();
main();
break;
case 2:
s.pop();
main();
break;
case 3:
s.display();
main();
break;
case 4:
break;
}
}

3. ABSTRACT DATA TYPE - QUEUE


Program

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define max 100

class queue
{
private:
int a[max],front,rear,data;
public:
queue() {
front = rear = 0;
}
void enqueue();
void dequeue();
void display();
};

void queue::enqueue() {
if(rear > 100)
cout<<"\nQueue is full\n";
else {
cout<<"\nEnter the data : ";
cin>>data;
a[rear++] = data;
}
getch();
}
void queue::dequeue() {
if(front == rear)
cout<<"\nQueue is empty\n";
else
cout<<"Dequeued element : "<<a[front++];
getch();
}

void queue::display() {
if(front == rear)
cout<<"\nQueue is empty\n";
else {
cout<<"\nQueue elements\n";
for(int i = front;i<rear;i++)
cout<<a[i]<<endl;
}
}

void main() {
int ch;
queue q;
clrscr();
do {
cout<<"\n1. Enqueue\t2. Dequeue\t3. Display\t4. Exit\n";
cout<<"Your choice : ";
cin>>ch;
switch(ch) {
case 1:
q.enqueue();
break;
case 2:
q.dequeue();
break;
case 3:
q.display();
break;
case 4:
exit(0);
break;
default:
cout<<"\nInvalid choice.\n";
}
}while(ch!=4);
}
4. CONSTRUCTORS AND DESTRUCTORS
Program

//Constructor and Destructor


#include<iostream.h>
#include<conio.h>

class myclass
{
int a,b;
public:
myclass()
{
cout<<"\nIn default constructor\n";
a = 10;
b = 20;
}
myclass(int m,int n)
{
cout<<"\nIn parameterised constructor\n";
a = m;
b = n;
}
~myclass()
{
cout<<"\nDestructor invoked\n";
}
void show()
{
cout<<"\nValue of a : "<<a<<endl;
cout<<"\nValue of b : "<<b<<endl;
}
};

void main()
{
clrscr();
myclass m;
m.show();
myclass n(50,100);
n.show();
getch();
}

4. CONSTRUCTOR OVERLOADING
Program

//Constructor Overloading
#include<iostream.h>
#include<conio.h>

class example
{
float p,n,r,i;
public:
example(float m,float y)
{
p = m;
n = y;
r = 12;
}

example(float x,float y,float z)


{
p = x;
n = y;
r = z;
}

void calculate()
{
i = (p*n*r)/100;
cout<<"\nInterest amount : "<<i;
}
};

void main()
{
clrscr();
cout<<"\n- Constructor Overloading -\n\n";
cout<<"Passing two values to parameterised constructor :\n";
example e1(15000.0,5.0);
e1.calculate();
cout<<"\nPassing three values to parameterised constructor :\n";
example e2(20000.0,4.0,10);
e2.calculate();
getch();
}

5. STATIC MEMBERS
Program

//Static Memeber
#include<iostream.h>
#include<conio.h>

class stat
{
static int count;
int number;
public:
void getdata(int a)
{
number = a;
count++;
}
void outdata()
{
cout<<"\nCount : "<<count<<endl;
}
};

int stat::count;

void main()
{
stat x,y,z;
clrscr();
x.getdata(200);
y.getdata(300);
z.getdata(400);
x.outdata();
y.outdata();
z.outdata();
getch();
}
5. STATIC METHODS
Program

//Static Member
#include<iostream.h>
#include<conio.h>

class stat
{
private:
int no;
char name[10];
static int count;
public:
void getdata()
{
cout<<"enter no and name : ";
cin>>no>>name;
count++;
}
void showdata()
{
cout<<"No : "<<no<<endl;
cout<<"Name : "<<name<<endl;
}
static void showcount()
{
cout<<"no of data entered : "<<count;
}
};

int stat::count;
void main()
{
stat a,b,c;
clrscr();
a.getdata();
b.getdata();
c.getdata();
a.showdata();
b.showdata();
c.showdata();
stat::showcount();
getch();
}
6. BIT FIELDS
Program

//Bit Fields
#include<iostream.h>
#include<conio.h>

struct mybitfield
{
unsigned int weekday:3;
unsigned int year:8;
unsigned int month:4;
unsigned short totmnthday:6;
};

void main()
{
mybitfield z;
clrscr();
z.weekday = 18;
z.year = 89;
z.month = 2;
z.totmnthday = 30;
cout<<"weekday : "<<z.weekday<<endl;
cout<<"year : "<<z.year<<endl;
cout<<"month : "<<z.month<<endl;
cout<<"Total month days : "<<z.totmnthday;
getch();
}

7. BINARY OPERATOR OVERLOADING


Program

//Operator overloading
#include<iostream.h>
#include<conio.h>
class complex
{
int x,y;
public:
complex(){}
complex(int a)
{
x=a;y=a;
}
complex(int real, int img)
{
x=real;
y=img;
}
friend complex operator+(complex,complex);
void display(complex);
};

complex operator+(complex c1,complex c2)


{
complex c3;
c3=c1.x+c2.x;
c3.y=c1.y+c2.y;
return c3;
}
void complex::display(complex c)
{
cout<<c.x<<"+j"<<c.y<<endl;
}
int main()
{
clrscr();
complex c1,c2,c3;
c1=complex(2,3);
c2=complex(4);
c3=c1+c2;
cout<<"\n\n\n\t-----;(binary operator overloading);---";
cout<<"\n\n\t\t\t c1=";c1.display(c1);
cout<<"\n\t\t\t c2=";c2.display(c2);
cout<<"\n\t\t\t c3=";c3.display(c3);
getch();
return 0;
}
10. FUNCTION TEMPLATES
Program

//Function Template
#include<iostream.h>
#include<conio.h>
template<class t>
void bubble(t a[],int n)
{
for(int i=0;i<n-1;i++)
{
for(int j=n-1;i<j;j--)
{
if(a[j]<a[j-1])
{
swap(a[j],a[j-1]);
}
}
}
}
template<class x>
void swap(x &a,x &b)
{
x temp=a;
a=b;b=temp;
}
int main()
{
clrscr();
int x[5]={10,50,30,40,20};
float y[5]={1.1,5.5,3.3,4.4,2.2};
bubble(x,5);
bubble(y,5);
cout<<"sorted x_array:";
for(int i=0;i<5;i++)
cout<<x[i]<<" ";
cout<<endl;
cout<<"sorted y-array:";
for (int j=0;j<5;j++)
cout<<y[j]<<" ";
cout<<endl;
return 0;
}
11. CLASS TEMPLATES
Program

//Class template
#include<iostream.h>
#include<conio.h>
template <class r>
class test
{
r data;
public:
void getdata();
void putdata();
};
template<class r>
void test<r>::getdata()
{
cout<<"enter the output:\n";
cin>>data;
}
template<class r>
void test<r>::putdata()
{
cout<<"data is"<<data<<endl;
}
void main()
{
clrscr();
test<int>t1;
test<double>t2;
t1.getdata();
t1.putdata();
t2.getdata();
t2.putdata();
getch();
}
12. SINGLE INHERITANCE
Program

#include<iostream.h>
class data {
protected:int a,b;
public:
void getdata();
void showdata();
};

class mod:public data {


protected:
int res;
public:
void mod_div();
void display();
};

void data::getdata() {
cout<<"enter the values"<<endl;
cin>>a>>b;
}

void data::showdata() {
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
}

void mod::mod_div() {
getdata();
res=a%b;
}

void mod::display() {
showdata();
cout<<"the result of a%b is:"<<res<<endl;
}

void main() {
mod m;
m.mod_div();
m.display();
}
12. MULTIPLE INHERITANCE
Program

//Multiple Inheritance
#include<iostream.h>
#include<conio.h>
class emp
{
protected:
int eno;
char ename[10];
public:
void getdata()
{
cout<<"enter the employee no and name";
cin>>eno>>ename;
}
void showdata()
{
cout<<"employee no:"<<eno<<endl;
cout<<"employee name:"<<ename<<endl;
}
};
class dept
{
protected:
int dept_no;
char dept_name;
public:
void getdept()
{
cout<<"enter dept no and name \n";
cin>>dept_no>>dept_name;
}
void display()
{
cout<<"department no:"<<dept_no<<endl;
cout<<"department name:"<<dept_name<<endl;
}
};
class sal:public emp,public dept
{
protected:
int hra,da,ta,sal;
public:
void getsal()
{
getdata();
getdept();
cout<<"enter hra,da ta amount \n";
cin>>hra>>da>>ta;
}
void salary()
{
void display1();
void display2();
sal+hra+da+ta;
cout<<"hra="<<hra<<endl;
cout<<"da="<<da<<endl;
cout<<"ta="<<ta<<endl;
cout<<"salary="<<ta<<endl;
}
};
void main()
{
sal s;
s.getsal();
s.salary();
}

12. MULTI-LEVEL INHERITANCE


Program

//Multilevel Inheritance
#include<iostream.h>
#include<conio.h>
class student {
protected:
int no;
char name[20];
public:
void getdata() {
cout<<"enter no and name";
cin>>no>>name;
}

void showdata() {
cout<<"no="<<no<<endl;
cout<<"name="<<name<<endl;
}
};

class mark:public student {


protected:
int m1,m2,m3,total;
float avg;

public:
void getmark() {
getdata();
cout<<"enter the marks";
cin>>m1>>m2>>m3;
}

void calc() {
showdata();
total=m1+m2+m3;
avg=total/3;
cout<<"m1="<<m1<<endl;
cout<<"m2="<<m2<<endl;
cout<<"m3="<<m3<<endl;
cout<<"total="<<total<<endl<<"avg="<<avg<<endl;
}
};

class sports:public mark {


protected:
char sp_name[10];
int sp_pnt;
public:
void getsp_det() {
getmark();
cout<<"enter sports name and points";
cin>>sp_name>>sp_pnt;
}

void display() {
calc();
cout<<"sports name="<<sp_name<<endl;
cout<<"sports point="<<sp_pnt<<endl;
}
};

void main() {
sports s;
s.getsp_det();
s.display();
}
12. HYBRID INHERITANCE
Program

//Hybrid Inheritance
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number = a;
}
void put_number(void)
{
cout<<"\n roll no:"<<roll_number<<"\n";
}
};
class test:public student
{
protected:
float part1,part2;
public:
void get_marks(float x,float y)
{
part1 = x;
part2 = y;
}
void put_marks()
{
cout<<"\n marks obtained:\n part1 = "<<part1<<"\n part2"<<part2;
}
};
class sports
{
protected:
float score;
public:
void get_score(float s)
{
score = s;
}
void put_score()
{
cout<<"\n sports wt:"<<score<<"\n\n";
}
};
class result: public test, public sports
{
float total;
public:
void display();
};
void result::display()
{
total = part1+part2+score;
put_number();
put_marks();
put_score();
cout<<"\n total score :"<<total<<"\n";
}
void main()
{
result student_1;
student_1.get_number(1234);
student_1.get_marks(27.5,33.0);
student_1.get_score(6.0);
student_1.display();
}

13. VIRTUAL FUNCTIONS


Program

//Virtual Functions
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\n display base";
}
virtual void show()
{
cout<<"\n shoe=w base";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\n display derived";
}
void show()
{
cout<<"\n show derived";
}
};
int main()
{
base b;
derived d;
clrscr();
base *bptr;
cout<<"\n bptr points to base \n ";
bptr->display();
bptr->show();
cout<<"\n\n bptr points to derived \n";
bptr=&d;
bptr->display();
bptr->show();
return 0;
}

You might also like