· Web viewTo Nurture and Harness talent for empowerment towards self empowerment towards self...

36
LAB MANUAL OBJECT ORIENTED PROGRAMMING LAB (IT- 202 F)

Transcript of  · Web viewTo Nurture and Harness talent for empowerment towards self empowerment towards self...

LAB MANUAL

OBJECT ORIENTED PROGRAMMING LAB(IT- 202 F)

(DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING)

(Reason behind the object oriented lab)

Mission

To develop Akido college of Engineering & Technology into a “ center of excellence” By:

Providing State – of – the art Laboratories, Workshops, Research and instructional facilities.

Encouraging students to delve into technical pursuits beyond their academic curriculum.

Facilitating Post – graduate teaching and research. Assisting in the best possible placement

Vision

To Nurture and Harness talent for empowerment towards self empowerment towards self actualization in all technical domains – both existing for the future.

(SOFTWARE AND HARDWARE SOFTWARE REQUIRMENTS)

HARDWARE REQUIRMENTS:

PROCESSOR: P-I

RAM: 256MB

HDD: 40GB

SPEED: 900 mhz

SOFTWARE REQUIRMENTS:

TURBO C++ / Borland

Guidelines for the Students :

1. Students should be regular and come prepared for the lab practice.

2. In case a student misses a class, it is his/her responsibility to complete that missed experiment(s).

3. Students should bring the observation book, lab journal and lab manual. Prescribed textbook and class notes can be kept ready for reference if required.

4.They should implement the given Program individually

5.While conducting the experiments students should see that their programs would Meet the following criteria:

Programs should be interactive with appropriate prompt messages, error messages if any, and descriptive messages for outputs.

Programs should perform input validation (Data type, range error, etc.) and give appropriate error messages and suggest corrective actions.

Comments should be used to give the statement of the problem and every function should indicate the purpose of the function, inputs and outputs

Statements within the program should be properly indented

Use meaningful names for variables and functions.

Make use of Constants and type definitions wherever needed.

6. Once the experiment(s) get executed, they should show the program and results to the instructors and copy the same in their observation book.

Practical of OOPs LAB:

1. Write a program to implement a class.2. Write a program that uses a structure to store the three parts of a phone number

separately3. Write a program to implement parameterized constructor.4. Write a program to implement destructor5. Write a program to implement Static class member.6. Write a program to implement Friend function.7. Write a program to implement unary operator.8. Write a program to implement binary operator.9. Write a program to implement public inheritance.10.Write a program to implement a class template.11.Write a program to implement virtual base class.

(Experiment: 1)

AIM: Write a program to implement a Class.

#include<iostream.h>#include<conio.h>class item{int number;float cost;public:void getdata(int a,float b);void putdata(void){cout<<"number:"<<number<<"\n";cout<<"cost:"<<cost<<"\n";}};void item::getdata(int a,float b){number=a;cost=b;}int main(){clrscr();item x;cout<<"\noobject x"<<"\n";x.getdata(100,299.95);x.putdata();item y;cout<<"\noobject y"<<"\n";y.getdata(200,175.50);y.putdata();return 0;}

OUTPUT:

(Experiment: 2)

AIM: Write a program that uses a structure to store the three parts of a phone number separately.

#include<iostream.h>#include<conio.h>struct phone{char area[10];char exchange[10];char number[10];};int main(){phone ph1={"212","767","8900"};phone ph2;clrscr();cout<<"\nenter your area code,exchange and number:";cin>>ph2.area>>ph2.exchange>>ph2.number;cout<<"\nmy number is ("<<ph1.area<<")"<<ph1.exchange<<"-"<<ph1.number;cout<<"\nyour number is ("<<ph2.area<<")"<<ph2.exchange<<"-"<<ph2.number;getch();return 0;}

OUTPUT:

(Experiment: 3)

Write a program to implement Parameterized Constructor.

#include<iostream.h>class integer{ int m,n; public: integer(int, int); void display(void) { cout<<"m="<<m<<"\n"; cout<<"n="<<n<<"\n"; }}; integer::integer(int x,int y) { m=x; n=y; } int main() { integer int1(0,100); integer int2=integer(25,75); cout<<"\nOBJECT1"<<"\n"; int1.display(); cout<<"\nOBJECT2"<<"\n"; int2.display(); return 0;}

OUTPUT:

(Experiment: 4)

Write a program to implement Destructors.

#include<iostream.h>int count=0;class alpha{public: alpha() { count++; cout<<"\nNo.of object created"<<count; } ~alpha() { cout<<"\nNo.of object destroyed"<<count; count--; } };int main(){ cout<<"\nENTER MAIN\n"; alpha A1, A2, A3, A4; { cout<<"\n\nENTER BLOCK1\n"; alpha A5; } { cout<<"\n\nENTER BLOCK2\n"; alpha A6; } cout<<"\n\nRE-ENTER MAIN\n"; return 0; }

OUTPUT:

(Experiment: 5)

Write a program to implement Static Class Member.

#include<iostream.h>#include<conio.h>class item{static int count;int number;public:void getdata(int a){number=a;count++;}void getcount(void){cout<<"count:";cout<<count<<"\n";}};int item::count;int main(){clrscr();item a,b,c;a.getcount();b.getcount();c.getcount();a.getdata(100);b.getdata(200);c.getdata(300);cout<<"After reading data"<<"\n";a.getcount();b.getcount();c.getcount();return 0;}

OUTPUT:

(Experiment: 6)

Write a program to implement Friend Function.

#include<iostream.h>#include<conio.h>class sample{int a;int b;public:void setvalue() {a=25;b=40;}friend float mean(sample s);};float mean(sample s){return float(s.a+s.b)/2.0;}int main(){clrscr();sample x; //object xx.setvalue();cout<<"Mean value="<<mean(x)<<"\n";return 0;}

OUTPUT:

(Experiment: 7)

Write a program to implement Unary Operator.

#include<iostream.h>#include<conio.h>class space{ int x; int y; int z; public: void getdata(int a,int b,int c); void display(void); void operator-(); }; void space:: getdata(int a,int b, int c ) { x=a; y=b; z=c; } void space::display(void) { cout<<x<<"\n"; cout<<y<<"\n"; cout<<z<<"\n"; } void space::operator-() { x=-x; y=-y; z=-z; } int main() { clrscr(); space S; S.getdata(10,-20,30); cout<<"S:"; S.display(); -S; cout<<"S:";

S.display(); return 0; }

OUTPUT:

(Experiment: 8)

Write a program to implement Binary Operator.

#include<iostream.h>#include<conio.h>class complex{float x;float y;public:complex(){}complex (float real, float imag){x=real;y=imag;}complex operator+(complex);void display(void);};complex complex:: operator+(complex c){complex temp;temp.x=x+c.x;temp.y=y+c.y;return(temp);}void complex::display(void){cout<<x<<"+j"<<y<<"\n";}int main(){clrscr();complex C1,C2,C3;C1=complex(2.5,3.5);C2=complex(1.6,2.7);C3=C1+C2;cout<<"C1=";C1.display();cout<<"C2=";C2.display();cout<<"C3=";C3.display();return 0;

OUTPUT:

(Experiment: 9)

Write a program to implement Public inheritance.

#include<iostream.h>

#include<conio.h>

class B

{

int a;

public:

int b;

void get_ab();

int get_a(void);

void show_a(void);

};

class D:public B

{

int c;

public:

void mul(void);

void display(void);

};

void B::get_ab(void)

{

a=5;b=10;

}

int B::get_a()

{

return a;

}

void B::show_a()

{

cout<<"a="<<a<<"\n";

}

void D::mul()

{

c=b*get_a();

}

void D::display()

{

cout<<"a="<<get_a()<<"\n";

cout<<"b="<<b<<"\n";

cout<<"c="<<c<<"\n\n";

}

int main()

{

clrscr();

D d;

d.get_ab();

d.mul();

d.show_a();

d.display();

d.b=20;

d.mul();

d.display();

return 0;

}

OUTPUT:

(Experiment: 10)

Write a program to implement a class template.

#include<iostream.h>const size=3;template< class T>class vector{ T*v; public: vector() { v=new T[size]; for(int i=0;i<size;i++) v[i]=0; } vector(T*a) { for(int i=0;i<size;i++)

v[i]=a[i]; } T operator*(vector &y) { T sum=0; for(int i=0;i<size;i++) sum+=this-> v[i]*y.v[i]; return sum; } }; int main() { int x[3]={1,2,3}; int y[3]={4,5,6}; vector<int>v1; vector<int>v2; v1=x; v2=y; int R=v1*v2; cout<<"R="<<R<<"\n"; return 0;}

OUTPUT:

(Experiment: 11)

Write a program for virtual base class

#include<iostream.h>

class student

{

protected:

int roll_number;

public:

void get_number(int a)

{

roll_number=a;

}

void put_number(void)

{

cout<<"Roll No:"<<roll_number<<"\n";

}

};

class test:virtual public student

{

protected:

float part1,part2;

public:

void get_marks(float x,float y)

{

part1=x;

part2=y;

}

void put_marks(void)

{

cout<<"Marks obtained:"<<"\n";

cout<<"Part1="<<part1<<"\n";

cout<<"Part2="<<part2<<"\n";

}

};

class sports :public virtual student

{

protected:

float score;

public:

void get_score(float s)

{

score=s;

}

void put_score(void)

{

cout<<"Sports wt:"<<score<<"\n\n";

}

};

class result:public test,public sports

{

float total;

public:

void display(void);

};

void result::display(void)

{

total=part1+part2+score;

put_number();

put_marks();

put_score();

cout<<"Total score:"<<total<<"\n";

}

int main()

{

result student_1;

student_1.get_number(678);

student_1.get_marks(30.5,25.5);

student_1.get_score(7.0);

student_1.display();

return 0;

}

Output: