Вы находитесь на странице: 1из 64

September 12

ASSIGNMENT

2011
OBJECT ORIENTED PROGRAMMING

ASSIGNMENT

2011

Submitted by
ASWIN SEKHAR .R

S5 CSE
Page ASWIN SEKHAR.R ROL NO: 05 S5CSE

ASSIGNMENT

2011

ROLL NO:05

BMCE

1. Program to count the number of person of age group between 5&15

#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,n,a[100]; cout<<"\nEnter a limit: "; cin>>n; cout<<"\n\nEnter age of "<<n<<" people:"; for(i=0;i<n;i++) cin>>a[i]; clrscr(); cout<<"\n\nEntered age between 5 and 50 are:\n"; for(i=0;i<n;i++) if(a[i]>=5&&a[i]<=50)

getch();
ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

cout<<a[i]<<"\n";

ASSIGNMENT
} OUTPUT Enter a limit 5 Enter age of 5 people 2 15 38 72 4

2011

Entered age between 5 and 50 are 15 38 72

2. Program to implement binary search


#include<iostream.h> #include<conio.h> void binsearch(int ar[],int size,int ele) {
Page ROL NO: 05 S5CSE

cin>>ele;
ASWIN SEKHAR.R

cout<<"\n ENTER THE ELEMENT TO BE FOUND :\n";

ASSIGNMENT

2011

int lb=0,ub=size-1,mid; for(;lb<ub;) { mid=(lb+ub)/2; if(ar[mid]==ele) {

//lb=>lower bound,ub=>upper bound

cout<<"\n SEARCH SUCCESSFUL"; break; } else if(ar[mid]<ele) ub=mid-1;

else if(ar[mid]>ele) lb=mid+1; }

if(ub<lb)

cout<<"\n SEARCH UNSUCCESSFUL";

ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

ASSIGNMENT
void sort(int ar[],int size) { int temp; //sorts the array in ascending array using bubble sort

2011

for(int i=0;i<size;i++) for(int j=0;j<size-i-1;j++) if(ar[j]>ar[j+1]) { temp=ar[j]; ar[j]=ar[j+1]; ar[j+1]=temp; } } void display(int ar[],int size) { for(int i=0;i<size;i++) cout<<'\n'<<ar[i]; } void input(int ar[],int size) { for(int i=0;i<size;i++) cin>>ar[i]; }

{
ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

void main()

ASSIGNMENT
clrscr(); int size; cout<<"\n ENTER THE NUMBER OF ELEMENTS REQUIRED IN THE ARRAY :"; cin>>size;

2011

int *ar=new int(size); cout<<"\n ENTER THE ELEMENTS OF THE ARRAY :\n"; input(ar,size); sort(ar,size); //takes the input from the array //sorts the array in the ascending order

binsearch(ar, size, ele); cout<<"\n ENTER THE ELEMENT TO BE FOUND :\n"; cin>>ele; getch(); } OUTPUT ENTER THE NUMBER OF ELEMENTS REQUIRED IN THE ARRAY : 6 ENTER THE ELEMENTS OF THE ARRAY : 53 27 19 55 101

ENTER THE ELEMENT TO BE FOUND:


ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

46

ASSIGNMENT
27 SEARCH SUCCESSFUL

2011

3. Program to implement non Fibonacci number with in a given number

#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,j,n,a[100],f,f1,f2,flag; cout<<"\nEnter a limit: "; cin>>n; cout<<"\n\nEnter "<<n<<" elements:"; for(i=0;i<n;i++) cin>>a[i]; clrscr(); cout<<"\n\nthe non fibonacci numbers are:"; for(i=0;i<n;i++) { flag=0; f1=1; f2=1; for(j=0;j<=a[i];j++)
ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

ASSIGNMENT
{ f=f1+f2; if(a[i]==f) flag=1; f1=f2; f2=f; } if(flag==0) cout<<a[i]<<"\n"; } getch(); } OUTPUT Enter a limit 7 Enter 7 element 4 1 9 0 6 7 5

2011

the non fibonacci number are


ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

ASSIGNMENT

2011

4 9 6 7

4. program to find the average of float array using pointers


#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,n; float *avg,*a[100],sum; cout<<"\nEnter a limit: "; cin>>n; cout<<"\n\nEnter "<<n<<"elements:"; for(i=0;i<n;i++) cin>>*a[i]; for(i=0;i<n;i++) sum=sum+(*a[i]); *avg=sum/n; cout<<"\n\nAverage is : "<<*avg; getch(); } OUTPUT
ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

10

ASSIGNMENT
Enter a limit: 5 Enter 5 elements: 20 21 22 23 24 Average is:22

2011

5.program to implement linear search

#include <iostream.h> int LinearSearch(int [], int, int); int main() { const int NUMEL = 10; int nums[NUMEL] = {5,10,22,32,45,67,73,98,99,101}; int item, location; cout << "Enter the item you are searching for: "; cin >> item; location = LinearSearch(nums, NUMEL, item); if (location > -1) cout << "The item was found at index location " << location
ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

11

ASSIGNMENT
<< endl; else cout << "The item was not found in the list\n"; return 0; } // this function returns the location of key in the list // a -1 is returned if the value is not found int LinearSearch(int list[], int size, int key) { int i; for (i = 0; i < size; i++) { if (list[i] == key) return i; } return -1; }

2011

OUTPUT Enter the item you are searching for: 10 The item was found at index location 2

ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

12

ASSIGNMENT
6.Program to implement linked list operation

2011

#include<iostream.h> class sll { private: int data; sll *next; public: sll* insert_one(int,sll*); sll* delete_one(int,sll*); void ftraverse(sll*); void rtraverse(sll*); void search(int,sll*); void function(); }; void sll::function() { cout<<******************************************\n; cout<<program to implement a singly linked list \n; cout<<******************************************\n; sll * head; head=NULL; cout<<\n\nMENU :\n; cout<<1)insertion\n <<2)deletion\n <<3)forward traversal\n <<4)reverse traversal\n <<5)search\n <<6)exit\n\n; cout<<Enter your option :; int opt; cin>>opt; int d; while(opt!=6) {
ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

13

ASSIGNMENT
switch(opt) { case 1: cout<<Enter data to the node :; cin>>d; head=insert_one(d,head); cout<<inserted\n; break; case 2: cout<<Enter the data to be deleted :; cin>>d; head=delete_one(d,head); break; case 3: cout<<The forward traversal is :\n; ftraverse(head); break; case 4: cout<<The reverse traversal is :\n; rtraverse(head); cout<<NULL\n; break; case 5: cout<<Enter the element to be searched :; int d; cin>>d; search(d,head); break; case 6: break; } cout<<\n\nMENU :\n; cout<<1)insertion\n <<2)deletion\n <<3)forward traversal\n <<4)reverse traversal\n <<5)search\n <<6)exit\n\n; cout<<Enter your option :; cin>>opt;
ASWIN SEKHAR.R ROL NO: 05

2011

S5CSE

Page

14

ASSIGNMENT
} } sll* sll::insert_one(int d,sll* s) { sll*NEW; NEW=new sll; NEW->data=d; NEW->next=NULL; if(s==NULL) s=NEW; else { sll*s1=s; while(s1->next!=NULL) s1=s1->next; s1->next=NEW; } return s; } sll* sll::delete_one(int d,sll* s) { if(s==NULL) { cout<<list empty \n; return NULL; } if(s->data==d) s=s->next; else { sll *s1=s; while( s1->next && s1->next->data!=d ) { if(s1->next->data==d) { cout<<deleted; s1->next=s1->next->next; return s;
ASWIN SEKHAR.R ROL NO: 05

2011

S5CSE

Page

15

ASSIGNMENT
} s1=s1->next; } cout<<not found; } return s; } void sll::ftraverse(sll* s) { sll * s1=s; while(s1!=NULL) { cout<<s1->data<< -> ; s1=s1->next; } cout<<NULL\n; } void sll::rtraverse(sll* s) { if(s==NULL) return; else rtraverse(s->next); cout<<s->data<<->; } void sll::search(int d,sll* s) { while(s!=NULL) { if(s->data==d) { cout<<found\n; return ; } s=s->next; } cout<< search unsuccess ful \n; }
ASWIN SEKHAR.R ROL NO: 05

2011

S5CSE

Page

16

ASSIGNMENT
void main() { sll list; list.function(); }

2011

output

*************************************** program to implement singly linked list *************************************** menu: 1> insertion 2> deletion 3> forward traversal 4> reverse traversal 5> search 6> exit enter your option: 1 enter the data to the node: 34 menu:

1> insertion 2> deletion

ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

4> reverse traversal

17

3> forward traversal

ASSIGNMENT
5> search 6> exit enter your option: 1 enter the data to the node:26

2011

menu:

1> insertion 2> deletion 3> forward traversal 4> reverse traversal 5> search 6> exit enter your option: 1 enter the data to the node:16

menu:

1> insertion 2> deletion 3> forward traversal 4> reverse traversal 5> search 6> exit enter your option: 2
ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

18

ASSIGNMENT
enter the data to b deleted: 16

2011

menu: 1> insertion 2> deletion 3> forward traversal 4> reverse traversal 5> search 6> exit enter your option: 3 The forward traversal is: 34->26-> null

menu:

1> insertion 2> deletion 3> forward traversal 4> reverse traversal 5> search 6> exit enter your option: 4 the reverse traversal is: 26->34->null

ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

19

ASSIGNMENT
menu:

2011

1> insertion 2> deletion 3> forward traversal 4> reverse traversal 5> search 6> exit enter your option: 5 enter the element to be searched:34 found

7.Program to find nCr

#include<iostream.h> #include<conio.h> long factorial(int);

main() { clrscr(); int n; int r; int nr;


ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

20

ASSIGNMENT
int factorial_n; int factorial_r; int factorial_nr; long ncr; cout<<"\n Enter the value of n = "; cin>>n; cout<<"\n Enter the value of r = "; cin>>r; factorial_n=factorial(n); factorial_r=factorial(r); nr=n-r; factorial_nr=factorial(nr); ncr=factorial_n/(factorial_r*factorial_nr); cout<<"\n nCr = "<<n<<" C "<<r<<" = "<<ncr<<endl; getch(); return 0; }

2011

OUTPUT Enter the value of n = 4 Enter the value of r = 2 nCr =4C2=6

ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

21

ASSIGNMENT
8.Program to find no of Lines in a paragraph

2011

#include <iostream.h> #include <fstream.h> using namespace std; int main () { char c; int num=0; ifstream is; is.open ("myfile.txt"); while (is.good()) { c = is.get(); if (c=='\n')num++; } is.close(); cout<<"Number of lines in file is "<<num<<endl; return 0; }

9.Program to find number of Vowels & constant.

#include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h>


ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

22

ASSIGNMENT
void main() { clrscr(); char str[50],ch; int i,l=0,v=0,c=0,b=0; cout<<"enter the string"; gets(str); l=strlen(str); for(i=0;i<l;i++) {

2011

if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='O'||str[i]=='o'||str[i]==' u'||str[i]=='U') v++; else if(str[i]=='\t'||str[i]==' ') b++; } c=l-(b+v); cout<<"\nno: of vowel"<<v<<"\nback space"<<b<<"\nconsonant"<<c; getch(); }

output

ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

no: of vowels 4

23

Enter the string: nothing in it

ASSIGNMENT
back space 3 consonant 7

2011

10.Program to implement Queue operation

#include<conio.h> #include<iostream.h> #include<process.h> #include<malloc.h> // Creating a NODE Structure struct node { int data; struct node *next; }; // Creating a class QUEUE class queue { struct node *frnt,*rear; public: queue() // constructure { frnt=rear=NULL; } void insert(); // to insert an element
ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

24

ASSIGNMENT
void del(); // to delete an element void show(); // to show the stack }; // Insertion void queue::insert() { int value; struct node *ptr; cout<<"\nInsertion\n"; cout<<"Enter a number to insert: "; cin>>value; ptr=new node; ptr->data=value; ptr->next=NULL; if(frnt==NULL) frnt=ptr; else rear->next=ptr; rear=ptr; cout<<"\nNew item is inserted to the Queue!!!"; getch(); } // Deletion void queue::del() {
ASWIN SEKHAR.R ROL NO: 05

2011

S5CSE

Page

25

ASSIGNMENT
if(frnt==NULL) { cout<<"\nQueue is empty!!"; getch(); return; } struct node *temp; temp=frnt; frnt=frnt->next; cout<<"\nDeletion Operation........\nDeleted value is "<<temp->data; delete temp; getch(); } void queue::show() { struct node *ptr1=frnt; if(frnt==NULL) { cout<<"The Queue is empty!!"; getch(); return; } cout<<"\nThe Queue is\n"; while(ptr1!=NULL) {
ASWIN SEKHAR.R ROL NO: 05

2011

S5CSE

Page

26

ASSIGNMENT
cout<<ptr1->data<<" ->"; ptr1=ptr1->next; } cout<<"END"; getch(); } int main() { clrscr(); queue q; int choice; while(1) { cout<<"\n-----------------------------------------------------------"; cout<<"\n\t\tQUEUE USING LINKED LIST\n\n"; cout<<"1:INSERTION\n2:DELETION\n3:DISPLAY QUEUE\n4:EXIT"; cout<<"\nEnter your choice(1-4): "; cin>>choice; switch(choice) { case 1: q.insert(); break; case 2: q.del();
ASWIN SEKHAR.R ROL NO: 05

2011

S5CSE

Page

27

ASSIGNMENT
break; case 3: q.show(); break; case 4: exit(0); break; default: cout<<"Please enter correct choice(1-4)!!"; getch(); break; } } return 0; }

2011

OUTPUT QUEUE USING LINKED LIST

1:INSERTION 2:DELETION 3:DISPLAY QUEUE 4:EXIT Enter your choice<1-4>:1 insertion


ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

28

ASSIGNMENT
Enter a number to insert:76 New item is inserted to the queue!!! .................................................... QUEUE USING LINKED LIST

2011

1:INSERTION 2:DELETION 3:DISPLAY QUEUE 4:EXIT Enter your choice<1-4>:2 Deletion operation......... Deleted value is 76 .................................................... QUEUE USING LINKED LIST

1:INSERTION 2:DELETION 3:DISPLAY QUEUE 4:EXIT Enter your choice<1-4>:1 Enter a number to insert:36 New item is inserted to the queue!!! .................................................... QUEUE USING LINKED LIST

ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

29

ASSIGNMENT
1:INSERTION 2:DELETION 3:DISPLAY QUEUE 4:EXIT Enter your choice<1-4>:1 Enter a number to insert:96 New item is inserted to the queue!!! .................................................... QUEUE USING LINKED LIST

2011

1:INSERTION 2:DELETION 3:DISPLAY QUEUE 4:EXIT Enter your choice<1-4>:3 The queue is 36->96->end

11.Program to reverse a string

#include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> void main()


ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

30

ASSIGNMENT
{ clrscr(); char str[50],ch; int i,l=0; cout<<"enter the string"; gets(str); l=strlen(str); for(i=0;i<l/2;i++) { ch=str[i]; str[i]=str[l]; str[l]=ch; l--; } cout<<"reversed string is "; puts(str); getch(); }

2011

output
enter the string aswin reversed string niwsa

ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

12.Program to find columns and rows Matrix sum

31

ASSIGNMENT

2011

#include <iostream.h> #include <conio.h> int main() { clrscr(); int A[10][10],m,n,x,y,sum=0; //Create a Matrix A cout << "Enter number of rows and columns in Matrix A : \n"; cin>>n>>m; cout << "Enter elements of Matrix A : \n"; for(x=1;x<n+1;++x) for(y=1;y<m+1;++y) cin>>A[x][y]; //Find sum of each row for(x=1;x<n+1;++x) { A[x][m+1]=0; for(y=1;y<m+1;++y) A[x][m+1]=A[x][m+1]+A[x][y]; } //Find sum of each column for(y=1;y<m+1;++y) { A[n+1][y]=0;
ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

32

ASSIGNMENT
for(x=1;x<n+1;++x) A[n+1][y]+=A[x][y]; }

2011

cout << "\nMatrix A, Row Sum (Last Column)" << " and Column Sum (Last Row) : \n"; for(x=1;x<n+1;++x) { for(y=1;y<m+2;++y) cout << A[x][y] << " cout << "\n"; } //Print sum of each column x=n+1; for(y=1;y<m+1;++y) cout << A[x][y] << " cout << "\n"; if(m==n) { for(x=1;x<m+1;x++) for(y=1;y<n+1;y++) if(x==y) sum+=A[x][y]; else if(y==m-(x+1)) sum+=A[x][y]; }
ASWIN SEKHAR.R ROL NO: 05 S5CSE

";

";

Page

33

ASSIGNMENT
cout << "Sum of diagonal elements is : " << sum << endl; getch(); return 0; }

2011

Output
Enter number of rows and columns in Matrix A: 2 2 Enter elements of matrix A : 1 2 2 4 Matrix A row sum<last column> and column sum<last row>: 1 2 3 2 4 6 3 6

Sum of diagonal element is 5

13.Program to perform Stack Operation

#include<iostream.h> #include<conio.h> #define size 5 class Stack { int a[size]; int top; public: Stack() { top=-1; }
ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

34

ASSIGNMENT
Stack operator + ( int num ) { if( top==size-1 ) { cout<<"\nStack overflow !!"; getch(); } else a[++top]=num; return(*this); } Stack operator - (int) { if( top==-1 ) cout<<"\nStack Underflow !"; else cout<<"\nDeleted item is : "<<a[top--];

2011

getch(); return(*this); } /* void display() { if( top==-1 )


ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

35

ASSIGNMENT
cout<<"\nStack is empty !"; else { cout<<"\nStack is ..\n"; for(int i=top; i>=0; i-- ) cout<<a[i]<<"\n"; } getch(); } friend ostream & operator<<( ostream & x,Stack & s ); }; ostream & operator << (ostream & x,Stack & s) { if( s.top==-1 ) cout<<"\nStack is empty !"; else { cout<<"\nStack is ..\n"; for(int i=s.top; i>=0; i-- ) cout<<s.a[i]<<"\n"; } getch(); return(x); } int main()
ASWIN SEKHAR.R ROL NO: 05

2011

S5CSE

Page

36

ASSIGNMENT
{ Stack s1; int ch; do { clrscr(); cout<<"1.push\n2.pop\n3.display\n4.Exit\nEnter ur choice:"; cin>>ch; switch( ch ) { case 1: int ele; cout<<"\nEnter the number : "; cin>>ele; s1=s1+ele; break;

2011

case 2: s1=s1-1; break;

case 3: cout<<s1; break; } }while( ch!=4 ); getch();


ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

37

ASSIGNMENT
return 0; }

2011

Output
1.push 2.pop 3.disply 4,exit Enter your choice: 1 Enter the no: 34 1.push 2.pop 3.disply 4,exit Enter your choice: 1 Enter the no:26 1.push 2.pop 3.disply 4,exit Enter your choice: 1 Enter the no:19 1.push 2.pop 3.disply 4,exit
ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

38

ASSIGNMENT
Enter your choice: 2 Deleted item is :34 1.push 2.pop 3.disply 4,exit Enter your choice: 3 Stack is :26 19

2011

14.Program to Sort a set of strings

#include <iostream.h> #include <stdlib.h> # include <string> void selectionSort(string array[ ], const int arraySize); int findSmallest(string array[ ], int startIndex, int endIndex); void swap(string& first, string& second); void print(string array[ ], const int arraySize); int main( ) { const int ARRAY_SIZE = 3; string someArray[ARRAY_SIZE] = {"pera", "aca", "mika"}; selectionSort(someArray, ARRAY_SIZE); print(someArray, ARRAY_SIZE); system("PAUSE"); return 0; }
Page S5CSE

void selectionSort(string array[ ], const int arraySize) {


ASWIN SEKHAR.R ROL NO: 05

39

ASSIGNMENT
int smallestIndex = 0; for(int i = 0; i < arraySize; i++) { smallestIndex = findSmallest(array, i, arraySize - 1); swap(array[i], array[smallestIndex]); } } int findSmallest(string array[ ], int startIndex, int endIndex) { int smallestIndex = startIndex; for(int i = startIndex + 1; i <= endIndex; i++) { if(array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex; } void swap(string& first, string& second) { string temp = first; first = second; second = temp; } void print(string array[ ], const int arraySize) { for(int i = 0; i < arraySize; i++) cout << array[i] << endl; }

2011

15.program to store students details using structure


#include<iostream.h> #include<conio.h>
ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

40

ASSIGNMENT
struct student { int rollno; char name[25]; int marks[6]; }st; void main() { clrscr(); int i; cout<<"\n\tENTER STUDENT DETAILS\n\n"; cout<<"Enter name: "; cin>>st.name; cout<<"\nEnter roll no: "; cin>>st.rollno; cout<<"Enter marks of 6 subjects: "; for(i=0;i<6;i++) cin>>st.marks[i]; clrscr(); cout<<"\n\tSTUDENT DETAILS\n\n"; cout<<"name: "; cout<<st.name; cout<<"\nroll no: "; cout<<st.rollno; cout<<"marks of 6 subjects: ";
ASWIN SEKHAR.R ROL NO: 05

2011

S5CSE

Page

41

ASSIGNMENT
for(i=0;i<6;i++) cout<<st.marks[i]<<"\n"; getch(); } OUTPUT ENTER THE STUDENT DETAILS Enter name :aswin Enter Roll no:505 Enter the marks of 6 subject:23 21 24 21 23 20 STUDENT DETAILS Name:amal Roll no:504 Marks of 6 subjects: 23 21 24 21 23 20

2011

ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

42

ASSIGNMENT
16.Program to find Trace of matrix

2011

#includeiostream #includeconio.h using namespace std; int main() { int A[5][5],i,j,trace=0; cout << Enter a 3X3 matrix >> \n\n; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cin>>A[i][j]; //input values of a matrix } } cout << \n\nMatrix >> \n\n; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout << A[i][j] << ; //output values of a matrix } cout << \n\n; } for(i=0;i<3;i++) { trace+=A[i][i]; } cout << \n\nTrace of the matrix = << trace; getch(); return 0; }

ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

Output

43

ASSIGNMENT
Enter the 3X3 matrix 2 2 2 2 2 2 2 2 2 Matrix 2 2 2 2 2 2 2 2 2

2011

Trace of the matrix = 6

17.Program to implement operator overloading and function overloading.


#include<iostream.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; }
ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

// overload unary minus

44

ASSIGNMENT
void space :: display(void) { cout<<x<<" "; cout<<y<<" "; cout<<z<<"\n"; } void space :: operator-() { x=-x; y=-y; z=-z; } int main() { space S; S.getdata(10,-20,30); cout<< "S : "; S.display(); -S; cout<< "S : "; S.display(); }s // activates operator-() function

2011

OUTPUT: s : 10 -20 30
ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

45

ASSIGNMENT
s : -10 20 -30

2011

binary

#include<iostream.h> class complex { float x; float y; public: complex(){ } complex(float real,float image) { x=real; y=image; } 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";
ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

46

ASSIGNMENT
} int main() { 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(); } OUTPUT: C1 = 2.5 + j3.5; C2 = 1.6 + j2.7; C3 = 4.1 + j6.2;

2011

function overloading

#include<iostream.h> // Declarations (prototypes) int volume(int); double volume(double, int ); long volume(long, int, int); int main() {
ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

47

ASSIGNMENT
cout << volume(10) << "\n"; cout << volume(2.5,8) << "\n"; cout << volume(100L,75,15) "\n"; } int volume(int s) // cube { return(s*s*s); } double volume(double r, int h) { return(3.14519*r*h); } long volume(long l, int b, int h) { return(l*b*h); } OUTPUT: 1000 157.26 112500

2011

18. Program to implement private,public,protected inheritance


Public #include<iostream.h> class B
ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

48

ASSIGNMENT
{ 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()
ASWIN SEKHAR.R ROL NO: 05

2011

S5CSE

Page

49

ASSIGNMENT
{ 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() { D d; d.get_ab(); d.mul(); d.show_a(); d.display();

2011

d.b = 20; d.mul(); d.display(); }


ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

50

ASSIGNMENT

2011

OUTPUT: a=5 a=5 b = 10 c = 50 a=5 b = 20 c = 100

19.Program to implement 1. Single inheritance. 2. Multiple inheritance. 3. Hybrid inheritance. 4. Hierarchial inheritance. 5. Multilevel inheritance.

single

#include<iostream.h> class B { int a; public: int b; void get_ab(); int get_a(void); void show_a(void); };
ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

51

ASSIGNMENT
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()
ASWIN SEKHAR.R ROL NO: 05

2011

S5CSE

Page

52

ASSIGNMENT
{ cout << "a = " << get_a() << "\n"; cout << "b = " << b << "\n"; cout << "c = " << c << "\n\n"; } int main() { D d; d.get_ab(); d.mul(); d.show_a(); d.display();

2011

d.b = 20; d.mul(); d.display(); }

OUTPUT: a=5 a=5 b = 10 c = 50 a=5 b = 20


ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

53

ASSIGNMENT
c = 100

2011

multiple

include<iostream.h> class M { protected: int m; public: void get_m(int); };

class N {

protected:
int n; public: void get_n(int); }; class P : public M, public N { public: void display(void); };
ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

54

ASSIGNMENT
void M :: get_n(int x); { m=x; } void N :: get_n(int y) { n=y; } void P :: display(void) { cout << "m = " << m << "\n"; cout << "n = " << n << "\n"; cout << "m*n = " << m*n << "\n"; }

2011

int main() { P p;

p.get_m(10); p.get_N(20); P.display(); }

OUTPUT:
ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

55

ASSIGNMENT

2011

m = 10 n = 20 m*n = 200

hybrid

#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 : public student {
ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

56

ASSIGNMENT
protected: float part1, part2; public: void get_mark(float x, float y) { part1 = x; part2 = y; } void put_mark(void) { cout << "Marks obtained: " << "\n" << "Part1 = " << part1 << "\n" << "Part2 = " << part2 << "\n"; } }; class sports { protected: float square; public: void get_score(float S) { score = s; } void put_score(void) {
ASWIN SEKHAR.R ROL NO: 05

2011

S5CSE

Page

57

ASSIGNMENT
cout << "Sports wt: " << score << "\n\n"; } }; class result : public test, public sports { float total; public: void display(void); };

2011

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(1234); student_1.get_marks(27.5, 33.0); student_1.get_score(6.0);


ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

58

ASSIGNMENT
student_1.display(); }

2011

OUTPUT Roll No: 1234 Marks obtained: Part1 = 27.5 Part2 = 33 Sports wt : 6

Total Score: 66.5

multilevel
class student { protected: int roll_number; public: void get_number(int); void put_number(void); }; void student::get_number(int a) { roll_number=a; }
ASWIN SEKHAR.R ROL NO: 05 S5CSE Page

59

ASSIGNMENT
void student::put_number() { cout<<"Roll number: "<<roll_number<<"\n"; } class test:public student { protected: float sub1; float sub2; public: void get_marks(float,float); void put_marks(void); }; void test::put_marks() { cout<<"Marks in sub1= "<<sub1<<"\n"; cout<<"Marks in sub2= "<<sub2<<"\n"; } class result:public test { float total; public: void display(void); }; void::display(void)
ASWIN SEKHAR.R ROL NO: 05

2011

S5CSE

Page

60

ASSIGNMENT
{ total=sb1+sub2; put_number(); putmarks(); cout<<"Total="<<total<<"\n"; } int main() { result student1; //student1 created student1.get_number(111); student1.get_marks(75.0,59.5); student1.display(); return 0; }

2011

20.Create class called students whose data members are name,rollno,class and marks of 6 subjects. Write a c++ program to prepare mark list of student by defining the member function readdetails(),showdetails() and showmarklist().
#include<iostream.h> #include<conio.h> class student { int rno; char name[25];

ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

public:

61

int marks[6];

ASSIGNMENT
void readdetails(); void showdetails(); void showmarklist(); }st; void student::readdetails() { clrscr(); int i; cout<<"\n\tENTER STUDENT DETAILS\n\n"; cout<<"Enter name: "; cin>>st.name; cout<<"\nEnter roll no: "; cin>>st.rno; cout<<"Enter marks of 6 subjects: "; for(i=0;i<6;i++) cin>>st.marks[i]; } void student::showdetails() { clrscr(); cout<<"\n\tSTUDENT DETAILS\n\n"; cout<<"name: "; cout<<st.name; cout<<"\nroll no: "; cout<<st.rno;
ASWIN SEKHAR.R ROL NO: 05

2011

S5CSE

Page

62

ASSIGNMENT
} void student::showmarklist() { int j; cout<<"marks of 6 subjects: "; for(j=0;j<6;j++) cout<<st.marks[j3]<<"\n"; } void main() { st.readdetails(); st.showdetails(); st.showmarklist(); getch(); } OUTPUT ENTER THE STUDENT DETAILS Enter name :aswin Enter Roll no:505 Enter the marks of 6 subject:23 21 24 21 23 20
ASWIN SEKHAR.R ROL NO: 05

2011

S5CSE

Page

63

ASSIGNMENT
STUDENT DETAILS Name:amal Roll no:504 Marks of 6 subjects: 23 21 24 21 2 20

2011

ASWIN SEKHAR.R ROL NO: 05 S5CSE

Page

64

Вам также может понравиться