Sie sind auf Seite 1von 43

1

PROGRAM- 1 #include<iostream.h> void swap(int *a,int *b) { int c; c=*a; *a=*b; *b=c; } void main() { int a,b; cout<<"\n enter 1st element : "; cin>>a; cout<<"\n enter 2nd alement : "; cin>>b; swap(&a,&b); cout<<"\n a= "<<a<<"\n b= "<<b; }

OUTPUT:Enter 1st element : 10 Enter 2nd element : 20 a=20 b=10

PROGRAM-2

#include<iostream.h> class cube { public: float volume(float a) { return a*a*a; } }; void main() { cube b; float vol; vol=b.volume(10.7); cout<<"\n volume of cube: "<<vol; } OUTPUT:Volume of cube: 1225.04 PROGRAM-3

#include<iostream.h> int sum(int x,int y=5) { return x+y; } void main() { int a,b,c; a=10; c=sum(a); cout<<"\n a+b= "<<c; }

OUTPUT:a+b= 15

PROGRAM-4

#include<iostream.h> #include<stdio.h> class salary { friend int calsal(int bs,int hra,int bonus); }; int calsal(int bs,int hra,int bonus) { return bs+hra+bonus; } class manager { public: char name[20]; int salary; void get_name() { cout<<"\n Enter the name of manager:"; gets(name); } void put_data() { cout<<"\n "<<name; cout<<"\n Salary= "<<salary; } };

class executive { public: char name[20]; int salary; void get_name() { cout<<"\n enter the name of executive:"; gets(name); } void put_data() { cout<<"\n "<<name; cout<<"\n Salary= "<<salary; } }; void main() { manager m1; m1.get_name(); m1.salary=calsal(10000,1000,2000); executive e1; e1.get_name(); e1.salary=calsal(15000,500,500); m1.put_data();

e1.put_data(); }

OUTPUT:-

Enter the name of manager : Suresh Aaggarwal Enter the name of executive : Jatin Kumar Suresh Aaggarwal Salary= 13000 Jatin Kumar Salary= 16000

PROGRAM-5 #include<iostream.h>

class ABC { public: int a; void disp1() { a=10; cout<<"\n we are in disp1"; cout<<"\n a="<<a; disp2(); disp3(); } private: int b; void disp2() { b=20; cout<<"\n we are in disp2"; cout<<"\n b="<<b; } protected: int c; void disp3() { c=30; cout<<"\n we are in disp3"; cout<<"\n c="<<c; } };

void main() { ABC obj; obj.disp1(); }

OUTPUT:we are in disp1 a=10 we are in disp2 b=20 we are in disp3 c=30

PROGRAM-6 #include<iostream.h> class sum {

10

public: int add(int x,int y); }; inline int sum::add(int x,int y) { return x+y; } class operands:public sum { }; void main() { operands obj; int sum; sum=obj.add(50,100); cout<<"\n Sum= "<<sum; } OUTPUT:Sum= 150 PROGRAM-7 #include<iostream.h> class ABC { int a;

11

public: ABC() { a=20; } void show() { cout<<"\n a = "<<a; } }; void main() { ABC obj; obj.show(); } OUTPUT:a = 20 PROGRAM-8 #include<iostream.h> class ABC { int a; public:

12

ABC() { } ABC(int x) { a=x; } void show() { cout<<"\n a = "<<a; } }; void main() { ABC obj; obj=ABC(50); obj.show(); } OUTPUT:a = 50

13

PROGRAM-9 #include<iostream.h> class XYZ { int a,b; public: XYZ() {

14

cout<<"\n This is without parameter constructor."; } XYZ(int x) { a=x; cout<<"\n This is parameterized constructor."; cout<<"\n A = "<<a; } XYZ(int x,int y,int z=40) { b=x+y+z; cout<<"\n This is default argument constructor."; cout<<"\n B = "<<b; } }; void main() { XYZ obj1; XYZ obj2(10); XYZ obj3(20,30); } OUTPUT:This is without parameter constructor.

15

This is parameterized constructor. A = 10 This is default argument constructor. B = 90

PROGRAM-10 #include<iostream.h> class contr { public: int a,b; contr(int x,int y) { a=x;

16

b=y; } contr(contr &temp) { a=temp.a; b=temp.b; } void show() { cout<<"\n A="<<a; cout<<"\n B="<<b; } }; void main() { contr obj1(100,200); contr obj2(obj1); cout<<"\n In obj1:"; obj1.show(); cout<<\n After copy:; cout<<"\n In obj2:"; obj2.show(); } OUTPUT:In obj1:

17

A = 100 B = 200 After copy: In obj2: A = 100 B = 200

PROGRAM-11 #include<iostream.h> class complex { public: float x,y; complex() { } complex(float real,float imag)

18

{ x=real; y=imag; } complex operator+(complex ob) { complex temp; temp.x=x+ob.x; temp.y=y+ob.y; return temp; } void display() { cout<<x<<"+i"<<y<<"\n"; } }; void main() { complex c1,c2,c3; c1=complex(3.5,5.0); c2=complex(6.3,4.2); c3=c1+c2; cout<<"\n c1: "; c1.display(); cout<<"\n c2: "; c2.display(); cout<<"\n c3 = c1 + c2: "; c3.display(); }

19

OUTPUT:c1 : 3.5 + 5 i c2 : 6.3 + 4.2 I c3 = c1 + c2: 9.8 + 9.2 I

PROGRAM-12 #include<iostream.h> class complex { public: float x,y; complex() { } complex(float real,float imag) { x=real;

20

y=imag; } friendcomplex operator+(complex,complex); void display() { cout<<x<<"+i"<<y<<"\n"; } }; complex operator+(complex ob1,complex ob2) { complex temp; temp.x = ob1.x + ob2.x; temp.y = ob1.y + ob2.y; return temp; } void main() { complex c1,c2,c3; c1=complex(3.5,5.0); c2=complex(6.3,4.2); c3=c1+c2; cout<<"\n c1: "; c1.display(); cout<<"\n c2: "; c2.display(); cout<<"\n c3: "; c3.display(); }

21

OUTPUT:c1 : 3.5 + 5 i c2 : 6.3 + 4.2 I c3 = c1 + c2: 9.8 + 9.2 I

PROGRAM-13 #include<iostream.h> class unary { int x,y,z; public: void get_data(int a,int b,int c) { x=a; y=b; z=c; } void display() { cout<<"\n x = "<<x;

22

cout<<"\n y = "<<y; cout<<"\n z = "<<z; } void operator-() { x=-x; y=-y; z=-z; } }; void main() { unary s; s.get_data(6,-7,8); cout<<"\n s:"; s.display(); -s; cout<<"\n -s:"; s.display(); }

OUTPUT:-

s: x=6

23

y = -7 z=8 -s: x = -6 y=7 z = -8

PROGRAM-14 #include<iostream.h> class base { public: int a; void disp() { cout<<"\n This is class Base."; cout<<"\n A= "<<a; } }; class derived:public base { public: void access_data() {

24

a=100; disp(); } }; void main() { derived d; d.access_data(); } OUTPUT:-

This is class Base. A = 100

25

PROGRAM-15 #include<iostream.h> class A { public: void show1() { cout<<"\n This is class A"; } }; class B { public: void show2() { cout<<"\n This is class B"; } }; class C:public A,public B

26

{ public: void show3() { cout<<"\n This is class C"; } }; void main() { C obj; obj.show1(); obj.show2(); obj.show3(); }

OUTPUT:This is class A This is class B This is class C

27

PROGRAM-16 #include<iostream.h> class A { public: void show1() { cout<<"\n This is class A"; } }; class B:public A { public: void show2() { cout<<"\n This is class B"; } }; class C:public B { public:

28

void show3() { cout<<"\n This is class C"; } }; void main() { C obj; obj.show1(); obj.show2(); obj.show3(); }

OUTPUT:-

This is class A This is class B This is class C

29

PROGRAM-17 #include<iostream.h> class XYZ { public: int x; void func(int a) { cout<<"\n This is function of class XYZ"; x=a; cout<<"\n \"a = "<<a<<"\""; } }; void main() { XYZ obj; XYZ *ptr; ptr=&obj; ptr->func(50); }

30

OUTPUT:This is function of class XYZ a = 50

31

PROGRAM-18 #include<iostream.h> class ABC { public: int a; void assign() { this->a=500; cout<<"\n Value of a= "<<a; this->func(); } void func() { cout<<"\n This is function call by 'this' pointer"; } }; void main() { ABC obj; obj.assign(); }

32

OUTPUT:Value of a = 500 This is function call by 'this' pointer

33

PROGRAM-19 #include<iostream.h> #include<stdio.h> #include<string.h> void main() { char str[50]; int length; cout<<"\n Enter the string : "; gets(str); length=strlen(str); cout<<"\n The number of characters in String:"<<length; }

OUTPUT:Enter the string : Object Oriented Programming The number of characters in String : 27

34

PROGRAM-20 #include<iostream.h> class base { public: virtual void display() { cout<<"\n Display of class Base"; } void show() { cout<<"\n Show of class Base"; } }; class derived :public base { public: void display() { cout<<"\n Display of class Derived"; } void show() { cout<<"\n Show of class Derived"; } }; void main()

35

{ derived d; d.display(); d.show(); base *ptrb; ptrb=&d; ptrb->display(); ptrb->show(); } OUTPUT:-

Display of class Derived Show of class Derived Display of class Derived Show of class Base

PROGRAM-21

36

#include<iostream.h> class a { public: void disp1() { cout<<"\n This is class a"; } }; class b:virtual public a { public: void disp2() { cout<<"\n class b"; } }; class c:virtual public a { public: void disp3() { cout<<"\n class c"; } }; class d :public b,public c { public:

37

void disp4() { cout<<"\n class d"; } }; void main() { d od; od.disp1(); }

OUTPUT:-

This is class d

PROGRAM-22 #include<iostream.h>

38

#include<fstream.h> void main() { ofstream outf("item"); cout<<"\n Enter item name: "; char name[30]; cin>>name; outf<<name<<"\n"; cout<<"\n Enter item cost: "; float cost; cin>>cost; outf<<cost<<"\n"; outf.close(); ifstream inf("item"); inf>>name; inf>>cost; cout<<"\n"; cout<<"\n item name: "<<name; cout<<"\n item cost: "<<cost; inf.close(); }

OUTPUT:-

39

Enter item name: Basket Enter item cost: 145 item name: Basket item cost: 145

PROGRAM-23 #include<iostream.h> void main()

40

{ int count=0; char c; cout<<"\n INPUT TEXT : "; cin.get(c); while(c!='\n') { count++; cin.get(c); } cout<<"\n Number of characters = "<<count; }

OUTPUT:INPUT TEXT : C++ Project Number of characters = 11

PROGRAM-24 #include<iostream.h> class base

41

{ public: virtual int volume(int s) { return s*s*s; } }; class derived : public base { public: double volume(double r,int h) { return 3.14519*r*r*h; } long volume(long l,int b,int h) { return l*b*h; } }; void main() { derived obj; base *ptr; ptr=&obj; cout<<"\n 1.volume of cube."; cout<<"\n 2.volume of cylinder."; cout<<"\n 3.volume of rectangular box."; cout<<"\n\n Enter your choice : "; int ch;

42

cin>>ch; switch(ch) { case 1: int edge,vol1; cout<<"\n Enter the edge : "; cin>>edge; vol1=ptr->volume(edge); cout<<"volume is = "<<vol1; break; case 2: double vol2,r; int h1; cout<<"\n Enter the radius & hight : "; cin>>r>>h1; vol2=ptr->volume(r,h1); cout<<"\n volume is = "<<vol2; break; case 3: int b,h2; long l,vol3; cout<<"\n Enter the length,breath,hight : "; cin>>l>>b>>h2; vol3=ptr->volume(l,b,h2); cout<<"\n volume is = "<<vol3; break; } }

43

OUTPUT:-

1.volume of cube. 2.volume of cylinder. 3.volume of rectangular box. Enter your choice : 2 Enter the radius & hight : 5.7 8 volume is = 817.49

Das könnte Ihnen auch gefallen