0% found this document useful (0 votes)
33 views

Manual For OOP & DS - 000

The document contains details of 8 experiments on object-oriented programming concepts in C++. The experiments cover concepts like classes, objects, inheritance, operator overloading, function overloading, parameterized constructors, static members, friend functions and call by reference. Each experiment includes the program code, input, output and conclusion that the program was successfully executed.

Uploaded by

taazma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Manual For OOP & DS - 000

The document contains details of 8 experiments on object-oriented programming concepts in C++. The experiments cover concepts like classes, objects, inheritance, operator overloading, function overloading, parameterized constructors, static members, friend functions and call by reference. Each experiment includes the program code, input, output and conclusion that the program was successfully executed.

Uploaded by

taazma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 29

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR

DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

Experiment Number 1 Aim :Write a C++ program to implement the concept of class and object. Given Data: - class student:-roll number, name and address.

Program :#include<iostream.h> # include <conio.h> # include <stdio.h> Class student { private: int sno; char name[20]; char address[50]; public: void getdata() { cout << Enter Students Roll no= ; cin >> sno; cout << Enter Students Name= ; cin >> name; cout << Enter Students Address =; cin >> address; } void putdata() { cout << \n Students Roll no :<<sno; cout << \n Students Name :<<name;
OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

cout << Students Address: <<address; } }; void main() { clrscr(); student s; s.getdata(); s.putdata(); getch(); }

Output :Enter Students Roll no= 1 Enter Students Name =John Smith Enter Students Address= Wellington Square, OXFORD OX1 2JD,UK Students Roll no: 1 Students Name: John Smith Students Address: Wellington Square, OXFORD OX1 2JD,UK

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

Experiment Number 2(a) Aim :To create c ++ program using reference variable .

Program :#include<iostream.h> # include <conio.h> # include <stdio.h> void main() { Int num1; clrscr(); Cout << enter no ; Cin >> num1; int num 2=num1; Cout << \n Num1 is << num1; Cout << \n address of num1 is << & num1 ; Cout << \n num2 is << num 2; Cout << \n address of num 2 is << & num2; getch(); return(0); }

Output :Enter num 25 No. 1 is 25 Address of num 1 is ox8fb4ff4 Num2 is 25 Address of num 2 is ox8fb4ff4.
OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

Result :- Hence the above program is executed successfully. Experiment Number 2(b) Aim :To perform swapping program using call by reference.

Program :#include<iostream.h> # include < conio .h > # include <stdio .h > Swap ( int * ,int * ); Void main( ) { int a =10 ,b =20; Swap ( & a , &b); Cout << A = << a ; Cout << \n B = << b; getch ( ); } Swap ( int * x, int * y) { int *c ; *c =*y ; * y =* x ; *x =* c; }

Output :OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

A=20 B=10

Result :- Hence the above program is executed successfully. Experiment Number 3

Aim :Function .

To create a c ++ program using static data member and member

Program :#include<iostream.h> #include<conio.h> #include<stdio.h> class test { private : int code; static int count; public : void setcode() { code=++count; } void showcode() { cout<<"object number :"<<code<<"\n"; } static void showcount() { cout<<"count :"<<count<<"\n"; }
OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

}; int test:: count; int main() { test t1,t2; t1.setcode(); t2.setcode(); test::showcount(); t1.showcode(); t2.showcode(); getch(); }

Output :Count : 2 Object number :1 Object number : 2

Result :- Hence the above program is executed successfully.

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

Experiment Number 4(a) Aim :Write a C++ program to find the area of rectangle by using parameterized Constructor

Program :#include<iostream.h> #include<conio.h> #include<stdio.h> class CRectangle { int width, height; public: CRectangle (int,int); int area() { return (width*height); } }; CRectangle::CRectangle (int a, int b) { width = a; height = b; }

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

int main () { clrscr(); CRectangle rect (3,4); CRectangle rectb (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; getch(); }

Output :rect area: 12 rectb area: 30

Result :- Hence the above program is executed successfully.

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

Experiment Number 4(b) Aim :using inline function. To write a program to find the multiplication values and the cubic values

Program :#include<iostream.h> #include<conio.h> class line { public: inline float mul(float x,float y) { return(x*y); } inline float cube(float x) { return(x*x*x); }

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

}; void main() { line obj; float val1,val2; clrscr(); cout<<"Enter two values:"; cin>>val1>>val2; cout<<"\nMultiplication value is:"<<obj.mul(val1,val2); cout<<"\n\nCube value is :"<<obj.cube(val1)<<"\t"<<obj.cube(val2); getch(); }

Output :-

Result :- Hence the above program is executed successfully.

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

Experiment Number 5 Aim :To create c ++ program using friend function .

Program :#include<iostream.h> #include<stdio.h> #include<conio.h> class A; class B { private : int b; public : void setvalue(int i) { b=i; } friend void max(B,A); }; class A { private : int a; public : void setvalue(int i) { a=i;

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

} friend void max(B,A); }; void max(B m,A n) { if(m.b > n.a) cout<<"largest vaalue is :"<<m.b; else cout<<"largest value is :"<<n.a; } void main() { A a1; a1.setvalue(10); B b1; b1.setvalue(20); max(b1,a1); getch(); }

Output :Largest is : 20

Result :- Hence the above program is executed successfully.

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

Experiment Number 6 Aim :To create program using function overloading .

Program :# include < iostream .h > # include < conio.h > int area ( int ,int ); float area ( float ) ; float area ( float ,float ); void main ( ) { Clrscr ( ); int rectangle ; float circle , cylinder ; rectangle = area (4,5); cout << \n area of rectangle is : << rectangle ; circle = area (2.5 ) ; cout << \n area of circle is : << circle ; cylinder = area (20,4.5); cout << \n area of cylinder is : << cylinder ; getch ( ); } int area ( int a ,int b) { return ( a * b);
OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

} float area ( float r ) { return (3.14 * r * r ) ; } float area ( float a ,float b ) { return (2 * 3.14 * a * b) ; }

Output :Area of rectangle is : 20 Area of circle is : 19.625 Area of cylinder is : 565.2

Result :- Hence the above program is executed successfully.

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

Experiment Number 7 Aim :To create a C++ program using Operator Overloading.

Program :#include<iostream.h> #include<conio.h> class complex { int a,b; public: void getvalue() { cout<<"Enter the value of Complex Numbers a,b:"; cin>>a>>b; } complex operator+(complex ob) { complex t; t.a=a+ob.a;
OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

t.b=b+ob.b; return(t); } complex operator-(complex ob) { complex t; t.a=a-ob.a; t.b=b-ob.b; return(t); } void display() { cout<<a<<"+"<<b<<"i"<<"\n"; } }; void main() { clrscr(); complex obj1,obj2,result,result1; obj1.getvalue(); obj2.getvalue(); result = obj1+obj2; result1=obj1-obj2; cout<<"Input Values:\n"; obj1.display(); obj2.display(); cout<<"Result:"; result.display();
OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

result1.display(); getch(); }

Output :Enter the value of Complex Numbers a,b: 2 3 Enter the value of Complex Numbers a,b: 4 5 Input Values: 2+3i 4+5i Result:6+8i -2+-2i

Result :- Hence the above program is executed successfully.

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

Experiment Number 8 Aim :To create a C++ program using Inheritance.

Program :#include<stdio.h> #include<conio.h> #include<iostream.h> class A { int a; public: int b; void getdata() {

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

cout<<"enter the value of a:"; cin>>a; } void show_ab() { cout<<"value of a: "<<a; cout<<"value of b: "<<b; } int get_a() { return(a); } }; class B: public A { int result; public: void cal() { result=get_a()*b; } void show() { cout<<"multiplication is :"<<result; } }; void main() { clrscr(); int x; B b1; b1.getdata();
OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

cout<<"enter value of b"; cin>>x; b1.b=x; b1.cal(); b1.show_ab(); b1.show(); getch(); }

Output :Enter the value of a : 12 Enter the value of b: 12 Value of a:12 Value of b : 12 Multiplication is : 114

Result :- Hence the above program is executed successfully.

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

Experiment Number 9(a) Aim :Write a C++ program- To search an element from given array using Linear Search.

Program :#include<iostream.h> #include<conio.h> void main() { clrscr(); int ar[10],n,num,no; cout<<"Enter size of the array: "; cin>>n; cout<<"Enter array element: "<<endl; for(int i=0;i<n;i++) { cout<<"Enter element "<<i<<": "; cin>>ar[i]; } cout<<"Elements of array: "<<endl; for(i=0;i<n;i++) cout<<"Element at index number "<<i<<" is: "<<ar[i]<<endl; cout<<"Enter number to search: "; cin>>num; for(i=0;i<n;i++) { if(num==ar[i]) { cout<<"Found at index number: "<<i; no=0; break; } else {

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

no=1; continue; } } if(no==1) cout<<"Sorry your search returned NO results. :("; getch(); }

Output :Enter size of the array: 4 Enter array element: Enter element 0: 3 Enter element 1: 5 Enter element 2: 1 Enter element 3: 6 Elements of array: Element at index number 0 is: 3 Element at index number 1 is: 5 Element at index number 2 is: 1 Element at index number 3 is: 6 Enter number to search: 1 Found at index number : 2

Result :- Hence the above program is executed successfully.

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

Experiment Number 9(b) Aim :Write a C++ program- To search an element from given array using Binary Search.

Program :#include<conio.h> #include<iostream.h> void main() { clrscr(); int a[100],i,loc,mid,beg,end,n,flag=0,item; cout<<"How many element: "; cin>>n; cout<<"Enter (IN ORDER) the elements of array: "<<endl; for(i=0;i<n;i++) cin>>a[i]; cout<<"Enter the element to be searched: "; cin>>item; loc=0; beg=0; end=n-1; while((beg<=end)&&(item!=a[mid])) { mid=(beg+end)/2; if(item==a[mid]) { cout<<"Search successful :)"; loc=mid;

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

cout<<"Position of the Item: "<<loc+1; flag=flag+1; } else if(item<a[mid]) end=mid-1; else beg=mid+1; } if(flag==0) { cout<<"Search NOT sucessfull :( "; } getch(); }

Output :How many element: 4 Enter (IN ORDER) the elements of array: 4 6 8 9 Enter the element to be searched: 8 Search successful :)Position of the Item: 3

Result :- Hence the above program is executed successfully.

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

Experiment Number 10(a) Aim :To create a C++ program using Bubble Sort & Selection Sort.

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

Program :-

BUBBLE SORT

#include<stdio.h> #include<conio.h> #include<math.h> main() { int a[10],temp,size,i,j; clrscr(); printf("How many elements you want to enter "); scanf("%d",&size); for(i=0;i<size;i++) { printf(" the elements are :"); scanf("%d"&a[i]); } for(i=0;i<size;i++) { for(j=0;j<size-1;j++) { if(a[j]<a[j+1]) { temp=a[i]; a[i]=a[j+1]; a[j+1]=temp; } } } printf("the sorted array .\n"); for(i=0;i<size;i++)
OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

{ printf("%d\n",a[i]); } getch(); }

Output :5

SELECTION SORT

How many elements you want to enter The elements are : 45 65 85 75 23 The sorted array 23 45 65 75 85

Result :- Hence the above program is executed successfully.

OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

Experiment Number 10(b) Aim :To create a C++ program using Bubble Sort & Selection Sort.

Program :#include<stdio.h> #include<conio.h> #include<math.h> main() { int a[10],temp,size,i,j; clrscr(); printf("How many elements you want to enter "); scanf("%d",&size); for(i=0;i<size;i++) { printf(" the elements are :"); scanf("%d"&a[i]); } for(i=0;i<size;i++) { for(j=0;j<size-1;j++) { if(a[j]<a[j+1]) { temp=a[i]; a[i]=a[j+1]; a[j+1]=temp;
OOP & DS Practical

ANJUMAN COLLEGE OF ENGINEERING & TECHNOLOGY, NAGPUR


DEPARTMENT :-ELECTRONICS AND TELECOMMUNICATION ENGINEERING SUBJECT:OBJECT ORIENTED PROGRAMMING & DATA STRUCTURE

} } } printf("the sorted array .\n"); for(i=0;i<size;i++) { printf("%d\n",a[i]); } getch(); }

Output :How many elements you want to enter


5

The elements are : 45 65 85 75 23 The sorted array 23 45 65 75 85

Result :- Hence the above program is executed successfully.

OOP & DS Practical

You might also like