P. 1
OOPS

OOPS

|Views: 27|Likes:
Published by Hari Haran

More info:

Published by: Hari Haran on Mar 13, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

03/13/2011

pdf

text

original

Anna University (Syllabus) V Semester (EEE) Cs-1261 Object oriented programming LAB PROGRAMS C

++

EX.NO.1 Simple c++ programs A) To convert Fahrenheit to Celsius #include<iostream.h> #include<conio.h> class temp { float c,f; public: void getdata() { cout<<"Enter the temperature in farenheit:"; cin>>f; } void display() { c=(f-32)/1.8; cout<<"The temperature in celcius is:"<<c; } }; int main() { clrscr(); temp t; t.getdata(); t.display(); getch(); return 0; } B) To find the largest of three numbers

#include<iostream.h> #include<conio.h> class largest { int a,b,c; public: void getdata() { cout<<"Enter three numbers:"; cin>>a>>b>>c; } void display() { if((a>b)&&(a>c)) cout<<"The largest is:"<<a; if(b>c) { cout<<"The largest is:"<<b; } else cout<<"The largest is:"<<c; } }; int main() { largest l; clrscr(); l.getdata(); l.display(); getch(); return 0; } C) To find the grade calculation

cout<<"\nM2="<<m2. cout<<"\nM1="<<m1. } void calculate() { total=m1+m2+m3. cin>>name. float avg.total. } void display() { cout<<"\nRollno="<<rollno. avg=total/3.m2.h> #include<conio. cin>>m1>>m2>>m3.m3.#include<iostream. else if((avg>=75)&&(avg<90)) . if((m1>=35)&&(m2>=35)&&(m3>=35)) { if(avg>=90) cout<<"\nGrade=A+". char name[20]. cin>>rollno. cout<<"\nM3="<<m3. public: void getdata() { cout<<"Enter the rollno:". cout<<"\nName="<<name.m1. cout<<"\nEnter the three subject marks:". cout<<"\nEnter the name:".h> class grade { int rollno.

else cout<<"\nGrade=D". } }.display().h> class employee { int eno. else if((avg>=50)&&(avg<60)) cout<<"\nGrade=C". int main() { grade g. return 0..getdata(). g.. g.cout<<"\nGrade=A". char ename[20]. g..h> #include<conio...". } D) Get and print the employee details #include<iostream.calculate(). . clrscr().dno. getch().dname[20]. else if((avg>=60)&&(avg<75)) cout<<"\nGrade=B".eage. } else cout<<"\nThe student has failed..

cout<<"\nEage="<<eage. cout<<"\nEnter department no. cout<<"\nEname="<<ename. cin>>dno>>dname>>bs.getdata().calculate().2*bs)+(0. } void display() { cout<<"\nEno="<<eno.15*bs). cin>>eno>>ename>>eage. getch(). e.name and age:".float bs.name and salary:". e.gs. cout<<"\nDname="<<dname. } }. void main() { employee e. cout<<"\nGross salary="<<gs.display(). cout<<"\nDno="<<dno. } void calculate() { gs=bs+(0. public: void getdata() { cout<<"Enter employee no. clrscr(). e. cout<<"\nBasic salary="<<bs. } .

} string(char *s) { length=strlen(s).name). name=new char[length+1]. } void join(string &a. strcpy(name. name=new char[length+1].NO. int length.string &b).s). cout<<"\nLength="<<length. name=new char[length+1]. void string::join(string &a. strcpy(name. }.h> #include<string.EX.h> class string { char *name. public: string() { length=0.string &b) { length=a.length+b.h> #include<conio.2 A) String concatenation using dynamic memory allocation #include<iostream. . delete name. }void display() { cout<<name<<"\n".a.length.

display(). s2. name3.s2. } void main() { char *first="Object". string s1. string operator + (string s). public: string().join(s1. } B) String concatenation using operator overloading #include<iostream. s1.name). }.b.strcat(name.display(). void echo(). name2. getch().h> #include<conio.name2).h> class string { private: char str[20]. clrscr(). string(char *mystr).display(). string name1(first). s2.name2("Oriented").name3).display().name3("Programming"). name1.display(). .join(name1.h> #include<string. s1.

cout<<"OUTPUT:\n". } string::string(char *mystr) { strcpy(str. str2. } string::string() { strcpy(str.str.3 .NO.str.echo(). return temp.echo(). string str3.string string::operator + (string s) { string temp. } void string::echo() { cout<<str<<"\n". } int main() { clrscr(). return 0.mystr). str1. strcpy(temp. string str1="C++".str). getch().s. string str2="Programming"." ").str). strcat(temp. } EX. str3.echo(). str3=str1 + str2.

. } int main() { clrscr(). }. } complex operator + (complex c). temp. return(temp). temp.h> #include<conio.x=x+c. } void complex::display() { cout<<x<<"+j"<<y<<"\n". y=imag. complex complex::operator + (complex c) { complex temp.y. float imag) { x=real. void display().y=y+c.x. public: complex() { } complex(float real.y.Overloading binary operator #include<iostream.h> class complex { float x.

c2=complex(5. cin>>data1. class one { int data1. return 0. public: void getdata() { cout<<"\nEnter a number:".two b).display().5). getch().h> class two. cout<<"C1:\t".display().c2.2. }.NO. } friend int largest(one a. c3=c1+c2. cout<<"c2:\t".display(). c1.5).2. c3.2.4 A) To find the largest of two numbers using friend function #include<iostream.complex c1.2. cout<<"c3:\t". } EX. cout<<"OUTPUT:\n". class two . c2. c1=complex(3. complex c3.h> #include<conio.

b.data2.{ int data2.two b).data1. getch(). a. } friend int largest(one a. two b. clrscr().h> .getdata().data2) return a. } void main() { one a. } B) To calculate the total distance using friend function #include<iostream. }. else return b. cin>>data2. cout<<"\nThe largest is:"<<largest(a. public: void getdata() { cout<<"\nEnter a number:".two b) { if(a.getdata().data1>b. int largest(one a.h> #include<conio.b).

class db { float ft. cin>>ft. cin>>in. class dm { float m. public: void getdata() { cout<<"\nEnter the distance in ft:". cin>>cm. } void display(int x) { m=x/100. } friend float add(dm.5.in. cout<<"\nThe addition result in m="<<m. ft=ft*30. m=m*100. cm=x%100.db). public: void getdata() { cout<<"\nEnter the distance in m:".cm. cout<<"\nEnter the distance in in:". cout<<"\nThe addition result in cm="<<cm. } . cin>>m. in=in*2.class db. }. cout<<"\nEnter the distance in cm:".

in.ft+b. } friend float add(dm. cout<<"\nThe addition result in in="<<in. a. b. } void main() { float x. cout<<"\nThe addition result in ft="<<ft. dm a.getdata(). z=a.cm+b. float add(dm a. b.b). return z. } EX.m+a. cout<<"\nThe total distance="<<x.5.db b) { float z. a. db b. }.display(x).display(x). clrscr(). x=add(a.5 . getch(). in=p/2.getdata().void display(int x) { ft=x/30.db). float p=x%30.NO.

} octal operator + (octal).Adding two octal numbers #include<iostream. if(t.h> #include<conio.h> #include<iomanip.x. public: void getdata() { cout<<"Enter the number:".h> class octal { int x. . octal octal::operator + (octal o) { octal t. cin>>x.x==0) { t.h> #include<string. t.x=x+o. char s[10]. int i=0.h> #include<stdio. char r[20].x.r[i]=t. }. cout<<"Addition of two octal number is:"<<r. } void display() { cout<<"OUTPUT:\n".

x<8)&&(t. } if(t."%d". void main() { clrscr().r[i]=s[0].x/8.x)).o2.r[i]=s[0].x=t. i++. octal o1.x!=0)) { while(t. i++. t. t.sprintf(s. } if((t. } return t.x=t.o3. t.x>0) { sprintf(s.(t.r). }.x%8))."%d"."%d". t. t.r[i++]='\0'. strrev(t. i++. t. } t. .x>0) { sprintf(s.x>=8) { while(t.r[0]).r[i++]='\0'.r[i]='\0'. } t.x/8.t.r[i]=s[0].(t.

(poly &). o2. }.i>0. cin>>arr[0].o1. } EX.NO.i. } cout<<"Enter the constant:". poly operator . poly operator + (poly &). cin>>arr[i]. public: void getpoly().poly &).6 Adding and subtracting two polynomials #include<iostream. } poly poly::operator + (poly &p) . void poly::getpoly() { cout<<"Enter the order of the polynomial:".getdata().getdata().order.h> class poly { int arr[10]. friend ostream &operator <<(ostream &. getch().display().i--) { cout<<"Enter the coefficients of x^"<<i<<":".h> #include<conio. cin>>order. for(i=order. o3. o3=o1+o2.

} else { t.i<=p.(poly &p) { poly t.i++) arr[i]=0.i++) p.order=order.order) { t. if(order>p.order+1. for(i=p.i<=order.i<=order.order.order+1. for(i=p.arr[i]=0.order.order. } for(i=0.order. } for(i=0.i<=t.i<=t.order=order. return(t).i++) t. if(order>p.arr[i].order=p.i++) .order) { t.i<=p.arr[i]=0.order. for(i=order+1. for(i=order+1.arr[i]=arr[i]+p. } else { t.order.order=p.i++) arr[i]=0.i++) p. } poly poly::operator .{ poly t.

arr[i]<<"X^"<<i<<"(+)".getpoly().i--) dout<<b. po1. cout<<po2.order. po3=po1+po2.po2.arr[i]. } . cout<<"=0\n". cout<<"Expression 2:"<<"\n\a". clrscr(). cout<<po3. getch().arr[0]. return 0.getpoly(). return(t). cout<<"Subtraction:"<<"\n\a". poly &b) { for(int i=b. cout<<"OUTPUT:\n". return (dout). dout<<b. po4=po1-po2. cout<<po1.po3. cout<<"Expression 1:"<<"\n\a". po2.arr[i]=arr[i]-p. } int main() { poly po1. } ostream &operator<<(ostream &dout. cout<<"Addition:"<<"\n\a".po4. cout<<po4.t.i>0.

ci. } void display() { cout<<"Name="<<name<<"\n".NO. public: void getdata() { cout<<"Enter the name. r=0.n. cout<<"Accno="<<no<<"\n".r. cin>>name>>type>>no. cout<<"Type="<<type<<"\n".7 Managing bank account #include<iostream.h> class account { protected: char name[25].EX. } }. public: savacc() { bal=1000.h> #include<conio.w.type&no:".h> #include<math. } . type[25]. class savacc:public account { float p. n=5.bal. int no.15.

} else { cout<<"\nMoney cannot be withdrawn. cout<<"savings account balance="<<bal. account::display(). class curacc:public account . cin>>w. } void withdraw() { cout<<"\nYour current savings account balance is:"<<bal. cin>>p. cout<<"\nEnter the amount to be deposited:". ci=(p*pow((1+(r/100)).. if(w<=bal) { bal=bal-w. bal=bal+p.void deposit() { cout<<"\nYour current savings account balance is:"<<bal. account::display(). cout<<"Compound interest:"<<ci. cout<<"\nSavings account balance="<<bal. } } }. cout<<"\nThe amount withdrawn="<<w. cout<<"\nSavings account balance="<<bal.\a\a\n". account::display(). bal=bal+ci.n))-p. cout<<"\nEnter the withdrawal amount:"..

if(w>bal) { account::display().{ float bal. } void withdraw() { cout<<"\nA minimum of RS.500 has to retained in the balance. cout<<"\nYour current account balance is:"<<bal. cout<<"\nCurrent account balance="<<bal. if(bal>=500) { .d.penality. cout<<"\nThe money cannot be withdrawn. public: curacc() { bal=1000. cin>>w. } void deposit() { cout<<"\nYour current account balance is:"<<bal. cin>>d.\a".\n". } if(w<=bal) { bal=bal-w.w. cout<<"\nEnter the deposit amount:". cout<<"\nCurrent account balance="<<bal. account::display().. cout<<"\nEnter the amount to be withdrawn:".. bal=bal+d.

cont1. curacc c. account::display(). do { cout<<"1. bal=bal-penality.account::display(). cin>>ch.ch2.ch1. cout<<"\nPenality amount="<<penality. cout<<"\nCurrent account balance="<<bal. cout<<"\nCurrent account balance="<<bal. savacc s.cont. } else if(bal<500) { penality=(0.1*(500-bal)). void main() { int ch. cout<<"Enter your choice:". switch(ch) { case 1: s.getdata(). } } } }. do { .Current account\n".cont2. cout<<"\nThe amount withdrawn="<<w.Saving account\n2. cout<<"\nThe amount withdrawn="<<w. clrscr().

cin>>ch2. cin>>ch1.getdata().\aCheque facility not available. break. break. break.\n". } cout<<"\nTo continue in the same savings account press 1:". cout<<"\nEnter your choice:". }while(cont==1).Current withdrawal\n3.deposit(). switch(ch2) { case 1: c. break.deposit(). .Current deposit\n2. do { cout<<"\n1.\a\a"..cout<<"\n1.Current & cheque withdrawal\n".. case 3: cout<<"\nSorry. switch(ch1) { case 1: s. cin>>cont. cout<<"\nEnter your choice:". default: cout<<"\nOut of range. case 2: s. case 2: c.Current & cheque deposit\n2.Cheque\n"..withdraw().

public: void getno(int a) { rollno=a. break.NO. }while(cont1==1). } cout<<"\nTo continue in the same current account press 1:". cin>>cont2.h> #include<conio.h> class student { protected: int rollno.case 2: c.. default: cout<<"\nOut of range.".\n:". }while(cont2==1). } EX. cin>>cont1. getch(). default: cout<<"\nOut of range. } void putno() { cout<<"\nRollno="<<rollno. ..withdraw()..8 Virtual base class #include<iostream. break. } cout<<"\nTo continue with any of the account press 1:".

} void putscore() { cout<<"\nScore="<<score. } }. cout<<"\nM2="<<m2. public: void getscore(int a) { score=a. class sports:public virtual student { protected: int score.} }. } void putmarks() { cout<<"\nM1="<<m1. m2=b.m2.int b) { m1=a. public: void getmark(int a. } }. . class test:public virtual student { protected: int m1.

getch(). putscore().NO. total=m1+m2+score.99). void main() { result r. } EX.getno(10). cout<<"\nTotal="<<total.getmark(90.class result:public test.display(). r. r. putmarks(). r.getscore(78). clrscr().h> #include<conio.public sports { int total. public: void display() { putno().h> class shape { public: double x. } }. .9 Virtual function #include<iostream. r.y.

void getdata(double a. cout<<"\nArea of rectangle="<<rec. rec=x*y. } virtual void displayarea() { cout<<"\nArea". } }. class circle:public shape { . y=b. class rectangle:public shape { public: void displayarea() { double rec. tri=0.5*x*y. class triangle:public shape { public: void displayarea() { double tri. } }. cout<<"\nArea of triangle="<<tri.double b=0) { x=a. } }.

10 Pure virtual function . void main() { clrscr(). s=&r. double p. rectangle r. cout<<"\nEnter the values of p & q:". s->displayarea(). cir=3.q). triangle t. s->getdata(p.public: void displayarea() { double cir. s=&t. s=&c. cout<<"\nArea of circle="<<cir.q). s->getdata(p). } EX.q.NO. shape *s. cin>>p>>q. s->displayarea(). circle c. getch(). } }. s->displayarea().14*x*x. s->getdata(p.

h> #include<conio. public: book(char *s. cout<<"\nPrice="<<price.h> class media { protected: char title[20]. price=a. } virtual void display()=0. class tape:public media { .h> #include<string. } }. public: media(char *s.s). } void display() { cout<<"\nTitle="<<title. class book:public media { int pages. cout<<"\nPages="<<pages.#include<iostream. float price. float a) { strcpy(title. }. float a.a) { pages=p. int p):media(s.

void main() { char *title=new char[30]. cout<<"\nTitle:".pages). cin>>title. float a. } void display() { cout<<"\nTitle="<<title.price. float t):media(s. . //book details cout<<"\nEnter the book details:\n". } }. cout<<"\nPrice=". cin>>title. cout<<"\nPrice="<<price.time. clrscr(). cout<<"\nPrice:". //tape details cout<<"\nEnter the tape details:\n". cout<<"\nPlay time="<<time.float time.a) { time=t. cout<<"\nPages:". public: tape(char *s. cin>>price. float price. cin>>pages. cin>>price. book book1(title. int pages. cout<<"\nTitle:".

...Tape. list[0]->display(). media *list[2]. cout<<"\nMedia details\n"... cin>>time. list[1]->display(). cout<<"\n. list[1]=&tape1.\n". list[0]=&book1..price. getch()..cout<<"\nPlay time:"...... } . tape tape1 (title.\n". cout<<"\n\n.time).Book.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->