You are on page 1of 21

GOLD FIELD INSTITUTE OF TECH & MGMT

VILLAGE CHHAISA, BALLABHGARH

OBJECT ORIENTED PROGRAMMING USING C++

Submitted To: Mr. SHIV KUMAR

Submitted by:AMIT KUMAR ROLL No.10/BCA/15

INDEX
SR

PROGRAMS
WAP TO IMPLEMENT FUNCTION OVERLOADING WAP TO SHOW THE SEQUENCE OF INVOKING CONSTRUCTOR IN DERIVED CLASS WAP TO CALCULATE AVERAGE OF THREE NUMBERS USING A CLASS WAP TO IMPLEMENT STATIC MEMBER FUNCTIONS. WAP TO IMPLEMENT OPERATOR OVERLOADING WAP TO IMPLIMENT CONSTRUTOR OVERLOADING WAP TO IMPLIMENT MULTI LEVEL INHERITENCE WAP THAT USES A STRUCT CALLED POINT TO MODEL A POINT. DEFINE THREE POINTS, AND HAVE THE USER INPUT VALUES TO TWO OF THEM. THEN SET THREE POINT EQUAL TO THE SUM OF THE OTHER TWO, AND DISPLAY THE VALUE OF THE NEW POINT. USING THREE CLASSES, DESIGN A PROGRAM THA WILL ACCEPT DIMENSION OF A TRIANGLE OR RECTANGLE AND DISPLAY THE AREA. WAP THAT USES A SRTUCT TO STORE THREE PARTS OF A PHONE NO SEPARATELY.

DATE

SIGN

NO.
1.

2. 3. 4. 5.

6. 7. 8.

9.

10.

PROGRAM 1
WRITE A PROGRAM TO IMPLEMENT FUNCTION OVERLOADING
#include<iostream.h> #include<conio.h> float add(int,float); float add(float,int); void main() { clrscr(); cout<<result is<<add(11,11.5); cout<<result isadd(2.5,7); getch(); } float add(int j,float n) { return(j+n); } float add(float r,int s) { return(r+s); }

OUTPUT:
result is:22.5 result is:9.5

PROGRAM -2
WRITE A PROGRAM TO SHOW THE SEQUENCE OF INVOKING CONSTRUCTOR IN DERIVED CLASS
#include<iostream.h> #include<conio.h> Class a { Public: a() { Cout<<\na\n; } }; Class b: virtual public a { Public: b() { Cout<<\nb\n; } }; Class c: virtual public a { Public: c() { Cout<<\nc\n; } }; Class d:public c,public b {

Public: d() { Cout<<\nd\n; } }; void main() { Clrscr(); D t; Getch();


}

OUTPUT:
a c b d

PROGRAM-3
WRITE A PROGRAM TO CALCULATE AVERAGE OF THREE NUMBERS USING CLASS
#include<iostream.h> #include<conio.h> class avg { int a,b,c; float d; public: void getdata(void) { cout<<ENTER THE VALUE OF A:; cin<<a; cout<<ENTER THE VALUE OF B:; cin<<b; cout<<ENTER THE VALUE OF C:; cin<<c; } void showdata(void) { d=(float)(a+b+c+)\3; cout<<THE AVERAGE OF THE GIVEN VALUES IS:<<d; } }; void main() { clrscr();

avg a; a.getdata();a.showdata(); getch(); }

OUTPUT:

ENTER THE VALUE OF A: 4 ENTER THE VALUE OF B: 6 ENTER THE VALUE OF C: 2 THE AVERAGE OF GIVEN VALUES IS: 3

PROGRAM-4
WRITE A PROGRAM TO IMPLEMENT STATIC MEMBER FUNCTION
#include<iostream.h> #include<conio.h> class test { int code; static int count; public: void setcode(void) { code=++count; } static void showcount() { cout<<count=<<count; } void discoed() { cout<<code is=<<code; } }; int test::count; int main() { clrscr(); test t1.t2; t1.setcode(); t2.setcode(); test::showcount(); test t3; t3.setcode(); test::showcount(); t3.discode(); t1.discode(); t2.discode();

getch(); }
8

OUTPUT:
count=2 count=3 code is=3 code is=1 code is=2

PROGRAM-5
9

WRITE A PROGRAM TO IMPLEMENT OPERATOR OVERLOADING


#include<iostream.h> #include<conio.h> Class cpmplex { int x,y; public: void getvalue() { cout<<\n enter the value of x; cin>>x; cout<<\n enter the value of y; cin>>y; } void display() { cout<<\n the value of is:<<x; cout<<\n the value of is:<<y; } complex operator+(complex c) { complex complex :: operator+(complex c) { complex temp; temp.x=x+c.x; temp.y=y+c.y; return(temp); } void main() { clrscr(); complex c1,c2,c3; c1.getvalue(); c2.getvalue();

cout<<for 1st object<<endl; c1.display(); cout<<for 2nd object<<endl; c2.display(); cout<<for 3rd object( after addition)<<endl;
10

c3=c2+c1; c3.display(); getch(); }

OUTPUT:
Enter the values of x:4 Enter the values of y:5 Enter the values of x:2 Enter the values of y:5 For 1st object The value of x is:4 The value of y is:5 For 2nd object The value of x is :2 The value of y is:3 For 3rd object (after addition) The value of x is:6 The value of y is:8

PROGRAM-6

11

WRITE A PROGRAM TO IMPLEMENT CONSTRUCTOR OVERLOADING


#include<iostream.h> #include<conio.h> class code { int x,y; public: code() {} code(int a,int b=9) { x=a; y=b; } code(code &a) { x=A.x; y=A.y; } void display() { cout<<\n X IS:<<x; cout<<\n Y IS:<<y; } }; void main() { clrscr(); code i1(10,12); code i2(i1); code i3=i1; code i4;

I4=i1; cout<<for object i1<<endl; i1.display(); cout<<for object i2<<endl; i2.display();


12

cout<<for object i3<<endl; i3.display(); cout<<for objecti4<<endl; i4.display(); getch(); }

OUTPUT:
For object i1 X IS:10 Y IS:12 For object i2 X IS:10 Y IS:12 For object i3 X IS:10 Y IS:12 For object i4 X IS:10 Y IS:12

PROGRAM-7
WRITE A PROGRAM TO IMPLEMENT MULTILEVEL INHERITANCE
#include<iostream.h>
13

#include<conio.h> Class declaration Class roll { Public: Int r; Void getroll(int a) { R=a; } Void disp() { Cout<<\nroll no of student is:<<r; } }; Class marks:public roll { Protected: Int m1,m2; Public: Void getmks(int a,int b) { M1=a; M2=b; } Void dispmks() { Cout<<\n marks for 1st subject is:<<m1; Cout<\n marks for 2nd subject is:<<m2; } };

Class total:public marks { Int t; Public: Void sum() { T=m1+m2;


14

} Void display() { Disp(); Cout<<\n---marks of the student ---; Dispmarks(); Cout<\n total marks of the student :<<t; } }; void main() { Clrsec(); Int p,q,r; Total a; Cout<<\n enter the roll no:; Cin>>p; a.getroll(p); cout<<enter the marks for 1st subject; cin>>q; cout <<enter the marks for 2nd subject; cin>>r; a.getmks(q,r); a.sum(); a.display(); getch(); }

OUTPUT:

Enter the roll no:4 Enter the marks for 1st subject:56 Enter the marks for 2nd subject:78 Roll no of 2nd subject is:4 ---marks of the student is--Marks for 1st subject is:56
15

Marks for 2nd subject is:78 Total marks:134

PROGRAM-8
WRITE A PROGRAM THAT USES A STRUCT CALLED POINT TO MODEL APOINT.DEFINE THREE POINTS,AND HAVE THE USER INPUT VALUES TO TWO OF THEM. THEN SET THE THREE POINT EQUAL TO THE SUM OF THE OTHER TWO,AND DISPLAY THE VALUE OF THE NEW POINT.INTRACTION WITH THE PROGRAM MIGHT LOOK LIKE THIS: ENTER THE COORDINATE OF P1: 3 4 ENTER THE COORDINATE OF P2: 5 7 COORDINATE OF P1+P2 ARE: 8,11
16

#include<iostream.h> #include<conio.h> Structure declaration Struct point { Int x,y; }; Struct point p1,p2,p3; Main function Void main() { Clrscr(); Cout<<enter the x coordinate of p1:; Cin>>p1.x; Cout<<enter the y coordinate of p1:; Cin>>p1.y; Cout<<enter the x coordinate of p2:; Cin>>p2.x; Cout<<enter the y coordinate of p2:; Cin>>p2.y; Cout<<\n the x coordinate of p3 is:<<p1.x+p2.y; Cout<<\n the y coordinate of p3is:<<p1.x+p2.y; Getch(); }

OUTPUT:
Enter the x coordinate of p1:4 Enter the y coordinate of p1:3 Enter the x coordinate of p2:5 Enter the y coordinate of p2:6 Enter the x coordinate of p3:9 Enter the y coordinate of p3:9

17

PROGRAM-9
USING THREE CLASSES,DESIGN A PROGRAM THAT WILL ACCEPT DIMENSION OF A TRIANGLE OR RECTANGLE AND DISPLAY THE AREA.REMEMBER THE TWO VALUES GIVEN AS INPUT WILL BE TREATED AS THE LENGTH OF TWO SIDE IN THE CASE OF TWO RECTANGLE AND AS BASE AND HEIGHT IN THE CASE OF TRIANGLES AND USED AS FOLLOW: AREA OF RECTANGLE =X*Y AREA OF TRIANGLE = *X*Y #include<iostream.h> #include<conio.h> Class shape
18

{ Public: Double x,y; Void getdata() { Cout<<\n enter the value of x:; Cin>>x; Cout<<\n enter the value of y:; Cin>>y; }; Virtual displayarea() {} }; Class triangle:public shape { Double area; Public: Displayarea() { Area=(x*y)/2; Cout<<area of triangle is:<<area; } }; Class rectangle:public shape {

Public: Double area; Displayarea() { Area(x*y); Cout<<area of rectangle is:<<area; } }; void main() { Clrsrc(); Triangle t; Rectangle r; Shape*sh,s;
19

Sh=&s; Sh->getdata(); Sh=&t Sh->displayarea(); Sh=&r; Sh->displayarea(); Getch(); }

OUTPUT:
Enter the value of x:4 Enter the value of y:6 Area of triangle is:12 Area of rectangle is:24

PROGRAM-10
A PHONE NO,SUCH AS(212) 767-8900,CAN BE THOUGHT OF AS HAVING THREE PARTS:THE AREA CODE(212), TO EXCH(767) AND THE NO(8900). WAP THAT USES A STRUCT TO STORE THES THREE PARTS OF A PHONE NO SEPARATELY. CALL THE STRUCT PHONE. CREATE TWO STRUCT VARIABLE OF TYPE PHONE. INITIALIZE ONE, AND HAVE THE USER INPUT A NUMBER FOR THE OTHER ONE.THEN DISPLAY BOTH NUMBERS. THE INTERCHANGE MIGHT LOOK LIKE THIS: ENTER YOUR AREA CODE, EXCHANGE,AND NUMBER:415 555 1212 MY NUMBER IS (212)767-8900 YOUR NUMBER IS (415) 555-1212 #include<iostream.h> #include<conio.h>
20

#include<string.h> Struct phone { Int code,exch; Char no[10]; }; Void main() { Clrscr(); Struct phone p1,p2; P1.code=212; P1.exch=767; Strcpy(p1.no,8900); Cout<<please enter your area code; Cin>>p2.code Cout<<please enter your exchange; Cin>>p2.exch; Cout<<please enter your number; Cin>>p2.no; Cout<<my no is:<<(<<p1.code<<)<<p1.exch<<<<p1.no; Cout<<your no is:<<(<p2.code<<)<<p2.exch<<-<<p2.no; Getch(); }

OUTPUT:
Please enter your area code:111 Please enter your exchange:734 Please enter your no:4567 My no is:(212)767-8900 Your no is(111)734-4567

21