You are on page 1of 25

PRACTICAL-1 AIM: Write a program to take name, address as character type, age as int, salary as float and contains

inline functions to set the value and display it.


SOURCE CODE: #include<iostream.h> #include<conio.h> #include<stdio.h> class data { public: char name[10],address[20]; int age; float salary; inline void getdata(); inline void showdata(); }; void data::getdata() { cout<<"\nEnter your Name:\t"; cin>>name; cout<<"\nEnter your Address:\t"; cin>>address; cout<<"\nEnter your age:\t"; cin>>age; cout<<"\nEnter your Salary:\t"; cin>>salary; } void data::showdata() { clrscr(); cout<<"\nHere are YOUR Details:\n"; cout<<"\nName:\t"; cout<<name; cout<<"\nAddress:"; cout<<address; cout<<"\nAge:\t"; cout<<age; cout<<"\nSalary:\t"; cout<<salary; } void main() { data d; clrscr();

d.getdata(); d.showdata(); getche(); }

OUTPUT

Enter your Name: SUNEET Enter your Address: C-2\121 Enter your age: 20 Enter your Salary: 6000000

Here are YOUR Details: Name: SUNEET Address: C-2\121 Age: 20 Salary: 6000000

PRACTICAL-2 AIM: Write a program of FUNCTION OVERLOADING for calculating the area of Triangle,Circle and Rectangle.
SOURCE CODE: #include<iostream.h> #include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> float area(float a,float b,float c) { float s,ar; s=(a+b+c)/2; ar=sqrt(s*(s-a)*(s-b)*(s-c)); cout<<"\nArea Of Triangle is:\t\n"; cout<<ar; return 0; } float area(float r) { const pi=3.14; float ar; ar=pi*r*r;

cout<<"\nArea Of Circle is:\t\n"; cout<<ar; return 0; } float area(float l,float b) { float ar; ar=l*b; cout<<"\nArea Of Rectangle is:\t\n"; cout<<ar; return 0; } void main() { float a,b,c,r,l; int ch;char x; do { clrscr(); cout<<"\nMENU\n"; cout<<"\n1.AREA OF TRIANGLE\n"; cout<<"\n2.AREA OF CIRCLE\n"; cout<<"\n3.AREA OF RECTANGLE\n"; cout<<"\n4.EXIT\n"; cout<<"\nEnter your choice:\n"; cin>>ch; switch(ch) { case 1:clrscr(); cout<<"\nEnter three sides of TRIANGLE:\n"; cin>>a>>b>>c; area(a,b,c); break; case 2:clrscr(); cout<<"\nEnter Radius of CIRCLE:\n"; cin>>r; area(r); break; case 3:clrscr(); cout<<"\nEnter Length and Breadth of RECTANGLE:\n"; cin>>l>>b; area(l,b); break; case 4:exit(0); case 5:cout<<"\nWrong Choice\n"; break; } cout<<"\nWant to continue(y/n):" ; cin>>x; }while(x=='y'||x=='Y');

getche(); }

OUTPUT MENU 1.AREA OF TRIANGLE 2.AREA OF CIRCLE 3.AREA OF RECTANGLE 4.EXIT enter your choice: 1

Enter three sides of TRIANGLE: 3 4 5 Area Of Triangle is: 6 Want to continue(y/n): y

MENU 1.AREA OF TRIANGLE 2. AREA OF CIRCLE 3.AREA OF RECTANGLE 4.EXIT Enter your choice: 2

Enter Radius of CIRCLE: 2 Area Of Circle is: 12 Want to continue(y/n):y

MENU 1.AREA OF TRIANGLEa 2.AREA OF CIRCLE 3.AREA OF RECTANGLE 4.EXIT Enter your choice: 3 Enter Length and Breadth of RECTANGLE:

4 3 Area Of Rectangle is: 12 Want to continue(y/n):n PRACTICAL NO 3 AIM: W.a.p to raise the number m to power n.the function takes the double value of m and int value of n.Use default value of n to make the program calculate the squares when this argument is omitted. SOURCE CODE:
#include <iostream.h>

#include <conio.h> #include <stdio.h> #include <stdlib.h> void power(double m,int n) { double p;p=1.0; for(int i=1;i<=n;i++) { p=p*m; } cout<<"\nThe Result is:\t"<<p; } void main() { clrscr(); double a;int b; char ch,x; do { clrscr(); cout<<"\nEnter the number:\n"; cout<<"m:";cin>>a; cout<<"\n Want to enter Power or want to omit it(y/n):\n"; cin>>x; if(x=='n'||x=='N') { power(a,2); } else if(x=='y'||x=='Y') { cout<<"\nEnter the power:\n"; cout<<"n:";cin>>b; power(a,b); } else cout<<"\n Wrong choice! "; cout<<"\n Want to continue(y/n):";

cin>>ch; }while(ch=='y'||ch=='Y'); }

OUTPUT

Enter the number: m:4 Want to enter Power or want to omit it(y/n): Y Enter the power: n:3 The Result is: 64 Want to continue(y/n):Y

Enter the number: m:5 Want to enter Power or want to omit it(y/n): N The Result is: 25 Want to continue(y/n): N

PRACTICAL N0 4 AIM:Create a class TIME with members hous,minutes,seconds.Take input,add two TIME objects passing objects to functions and display results. SOURCE CODE:
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> class TIME { int hrs,min,sec; public: TIME():hrs(0),min(0),sec(0) { } void get(int h, int m, int s) { hrs=h; min=m; sec=s;

} void display(void) const ; void sum(TIME,TIME); }; void TIME::display() const { cout<<hrs<<":"<<min<<":"<<sec; } void TIME::sum(TIME t1, TIME t2) { sec=t1.sec+t2.sec; if(sec>59) { sec-=60; min++; } min += t1.min+t2.min; if(min>59) { min-=60; hrs++; } hrs +=t1.hrs+t2.hrs; } void main() { clrscr(); const TIME obj1,obj2; TIME obj3; obj1.get(5,59,59); obj2.get(4,30,30); obj3.sum(obj1,obj2); cout<<"\n TIME= "; obj3.display(); cout<<"\n"; getche(); }

OUTPUT TIME= 10:30:29

PRACTICAL NO 5
AIM: Write a Program for multiplication of two matrices using OOPS. SOURCE CODE:
#include <iostream.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> class matrix { int a[4][4],b[4][4],c[4][4]; int m,n,r; public: void getdata(); void mul(); }; void matrix::getdata() { int i,j; cout<<"\nEnter the order of matrix as follows:"; cin>>m>>n>>r; cout<<"\n Enter the Elements of matrix A:"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>a[i][j]; } } cout<<"\n Enter the Elements of matrix B:"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>b[i][j]; } } } void matrix::mul() { cout<<"\nThe Resultant Matix is: \n"; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) {

c[i][j]=0; for(int k=0;k<r;k++) { c[i][j]=a[i][k]*b[k][j]; } cout<<c[i][j]<<" "; cout<<"\n"; } } } void main() { matrix obj; char ch; do { clrscr(); obj.getdata(); obj.mul(); cout<<"\n Want to continue(y/n):" ; cin>>ch; }while(ch=='y'||ch=='Y'); }

OUTPUT Enter the order of matrix as follows: 2 2 2 Enter the Elements of matrix A: 1 2 1 2 Enter the Elements of matrix B: 1 2 1 2 The Resultant Matix is: 2 4 2 4 Want to continue(y/n):N

PRACTICAL NO 6 AIM: Create a class student which has data members as name,branch,roll no,age,sex,marks in five subjects. Dsiplay the name os student and his percentage who has marks more than 70%.Use array of objects. SOURCE CODE:
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> #include<dos.h> class stud { char name[12],sex[4],branch[6]; int roll_no,age; float marks[6],sum,avg; public: stud():sum(0){} void getdata(); void showdata(); }; void stud::getdata() { cout<<"\n Enter your Name:";cin>>name; cout<<"\n Enter your Branch:";cin>>branch; cout<<"\n Enter your Roll no:";cin>>roll_no; cout<<"\n Enter your Age:";cin>>age; cout<<"\n Enter your Sex:";cin>>sex; cout<<"\n Enter your Marks in 5 subjects in any order:"; for(int i=1;i<=5;i++) { cout<<"\n Subject"<<i<<":"; cin>>marks[i]; } } void stud::showdata() { for(int i=1;i<=5;i++) { sum=sum+marks[i]; } avg=sum/5; if(avg>70) { cout<<"\n Name:"<<name; cout<<"\n Branch:"<<branch; cout<<"\n Roll no:"<<roll_no;

cout<<"\n Age:"<<age; cout<<"\n Sex:"<<sex; cout<<"\n Average:"<<avg<<"%"; for(int i=1;i<=5;i++) { cout<<"\n \n Subject"<<i<<":"<<marks[i]; } } } void main() { stud s[100]; char ch; int n,i; do { clrscr(); cout<<"\n How many student's data you want to Enter:"; cin>>n; clrscr(); cout<<"\n Enter the data: "; for(i=1;i<=n;i++) { cout<<"\n Student"<<i<<":"; s[i].getdata(); clrscr(); } clrscr(); cout<<"\n Please Wait While Processing.......... "; delay(1000); clrscr(); cout<<"\n Students who got over 70% are:"; for(i=1;i<=n;i++) { cout<<"\n"; s[i].showdata(); cout<<"\n"; } cout<<"\n Want to continue(y/n):"; cin>>ch; }while(ch=='y'||ch=='Y'); } OUTPUT

How many student's data you want to Enter:2 Enter the data: Student1: Enter your Name:SUNEET Enter your Branch:C Enter your Roll no:013 Enter your Age:19 Enter your Sex:M Enter your Marks in 5 subjects in any order: Subject1:98 Subject2:87 Subject3:88 Subject4:99 Subject5:99

Student2: Enter your Name: RAM Enter your Branch: C Enter your Roll no:14 Enter your Age:19 Enter your Sex:M Enter your Marks in 5 subjects in any order: Subject1:43 Subject2:54 Subject3:23 Subject4:43 Subject5:21

Students who got over 70% are: Name: SUNEET Branch: C Roll no:11 Age: 19 Sex: M Average: 94.199997% Subject1:98 Subject2:87 Subject3:88 Subject4:99 Subject5:99

Want to continue(y/n): N

PRACTICAL NO 7 AIM:W.a.p to access members of student class using pointer to object members(or using indirection operator.) SOURCE CODE: #include <iostream.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> class M { int x,y; public: void set_xy(int a,int b) { x=a; y=b; } friend int sum(M); }; int sum(M m) { int M::* px= &M :: x; int M::* py= &M :: y; M *pm=&m; int S; S=m.*px+pm->*py; return S; } int main() { M n; clrscr(); void (M::*pf)(int,int)=&M::set_xy; (n.*pf)(10,20); cout<<"\n SUM= "<<sum(n)<<"\n"; M *op=&n; (op->*pf)(30,40); cout<<"\n SUM= "<<sum(n)<<"\n"; return 0; getche(); }

OUTPUT SUM=30 SUM=70 PRACTICAL NO 8 AIM: W.a.p to enter any number and find its factoril using constructor. SOURCE CODE: #include <iostream.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> class factorial { int fct,n; public: factorial():fct(1) {} ~factorial(){} void fact(int); }; void factorial::fact(int x) { for(int i=x;i>=1;i--) { fct=fct*i; } cout<<"\n Factorial is:"<<fct; } void main() { factorial obj; int num; char ch; do { clrscr(); cout<<"\n Enter the number to find its Factorial: "; cin>>num; obj.fact(num); cout<<"\n Want to Enter more(y/n):"; cin>>ch; }while(ch=='y'||ch=='Y');

} OUTPUT Enter the number to find its Factorial: 6 Factorial is:720 Want to Enter more(y/n): Y Enter the number to find its Factorial: 5 Factorial is:120 Want to Enter more(y/n): N PRACTICAL NO 9 AIM: W.a.p to perform addition of two complex no.using constructor overloading.The first constructor which takes no arguments is used to create object when not initialized.second takes one arg and used to initialize the real and imaginary part &the third constructor takes two args and used to initialize real and imag of two different values. SOURCE CODE: #include<iostream.h> #include<conio.h> #include<stdio.h> class complex { float x,y; public: complex(){} complex(float a){x=y=a;} complex(float real,float imag) { x=real; y=imag; } friend complex sum(complex,complex); friend void show(complex); }; complex sum(complex c1,complex c2) { complex c3; c3.x=c1.x+c2.y; c3.y=c1.y+c2.y; return(c3); } void show(complex c) {

cout<<c.x<<"+i"<<c.y<<"\n"; } void main() { clrscr(); complex A(2.7,3.5); complex B(1.6); complex C; C=sum(A,B); cout<<"\n A= ";show(A); cout<<"\n B= ";show(B); cout<<"\n -----------"; cout<<"\n C= ";show(C); //Another way for initialization. cout<<"\n ~~~~~~~~~~~~~~~|"; complex p,q,r; p=complex(2.5,3.9); q=complex(1.6,2.5); r=sum(p,q); cout<<"\n"; cout<<"\n P= ";show(p); cout<<"\n Q= ";show(q); cout<<"\n -----------"; cout<<"\n R= ";show(r); getche(); }

OUTPUT A= 2.7+i3.5 B= 1.6+i1.6 -----------------C= 4.3+i5.1 ~~~~~~~~~~~ P= 2.5+i3.9 Q= 1.6+i2.5 -----------------R= 5+i6.4

PRACTICAL NO : 10 W.A.P TO GENERATE A FIBONACCI SERIES USING COPY CONSTRUCTOR.


#include <iostream.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> class Fibonacci { int f,f1,f2,m; public: Fibonacci() { } Fibonacci(int n) { m=n; } Fibonacci( Fibonacci &x) { x.f1=0; x.f2=1; cout<<"\n the required Fibonacci series is as follows: \n\n "; cout<<" "<<x.f1<<" "<<x.f2; for(int i=2;i<=x.m;i++) { x.f=x.f1+x.f2; cout<<" "<<x.f; x.f1=x.f2; x.f2=x.f; } } void display(); void display1(); void series(int); }; void main() { char ch; int n; do {

clrscr(); cout<<"\n Enter the length of series:"; cin>>n; clrscr(); Fibonacci A(n); Fibonacci B(A);

cout<<"\n \n Want to enter more(y/n): "; cin>>ch; }while(ch=='y'||ch=='Y'); }

OUTPUT
Enter the length of series :10 The Required Fibonacci series is as follows: 0 1 1 2 3 5 8 13 21 34 55 Want to enter more(y/n): N

PRACTICAL NO 11 AIM: create a class to keep the track of number of instances.use static data member,constructors and destructors to maintain updated information about active objects. SOURCE CODE: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> int c=0; class test { int code; static int count; public: test() { c++; cout<<"\n Number of objects created: "<<c; } ~test() {

cout<<"\n number of objects destroyed: "<<c; c--; } void setcode(void) { code=++count; } void showcode(void) { cout<<"\n Object number: "<<code<<"\n"; } static void showcount(void) { cout<<"count: "<<count<<"\n"; } }; int test::count; int main() { clrscr(); test t1,t2; cout<<"\n"; t1.setcode(); cout<<"\n"; t2.setcode(); test::showcount(); test t3; cout<<"\n"; t3.setcode(); test::showcount(); t1.showcode(); t2.showcode(); t3.showcode(); return 0; getche(); } OUTPUT No. of objects created: 1 No. of objects created: 2 Count: 2 No. of objects created: 3 Count:3 Object number:1 Object number:2

Object number:3 No. of objects destroyed: 3 No. of objects destroyed: 2 No. of objects destroyed: 1 PRACTICAL NO 12 AIM:W.a.p to demonstrate the use of this pointer. SOURCE CODE: #include <iostream.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> class alpha { int data; public: alpha() {} //no arg constructor alpha(int d) {data=d;} //one arg constructor void display() {cout<<data;} //display data alpha& operator=(alpha& a) { data=a.data; //not done automatically cout<<"\n Assignment Operator invoked:"; return *this; //return copy of this alpha } }; int main() { clrscr(); alpha a1(37); alpha a2,a3; a3=a2=a1; //invoke overload = twice cout<<"\n a2=";a2.display(); //display a2 cout<<"\n a3=";a3.display(); //display a3 cout<<"\n"; getche(); return 0; } OUTPUT

Assignment Operator invoked: Assignment Operator invoked: a2=37 a3=37 PRACTICAL NO 13 AIM: W.a.p to find biggest of three numbers using friend functions. SOURCE CODE:
#include <iostream.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> class big { int x,y,z; public: void getdata(); friend void find_big(big); }; void big::getdata() { cout<<"\n Enter three numbers to find the largest of them:\n"; cout<<"\n x=";cin>>x; cout<<"\n y=";cin>>y; cout<<"\n z=";cin>>z; } void find_big(big obj) { if(obj.x>obj.y&&obj.x>obj.z) cout<<obj.x<<" is bigger than "<<obj.y<<" and "<<obj.z; else if(obj.y>obj.x&&obj.y>obj.z) cout<<obj.y<<" is bigger than "<<obj.x<<" and "<<obj.z; else cout<<obj.z<<" is bigger than "<<obj.x<<" and "<<obj.y; } void main() { big obj2; char ch; do { clrscr(); obj2.getdata(); find_big(obj2); cout<<"\n"; cout<<"\n Want to continue(y/n):"; cin>>ch; }while(ch=='y'||ch=='Y');

} OUTPUT Enter three numbers to find the largest of them: x=456 y=678 z=654 678 is bigger than 456 and 654 Want to continue(y/n):Y Enter three numbers to find the largest of them: x=66 y=9999 z=432 999 is bigger than 66 and 432 Want to continue(y/n):N

PRACTICAL NO 14 AIM: W.a.p to demonstrate the use of friend function with inline assignment. SOURCE CODE: #include <iostream.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> class sample { int x,y; public: void getdata(); friend float mean(sample); }; void sample::getdata() { cout<<"\n Enter two numbers to find the mean of them:\n"; cout<<"\n x=";cin>>x; cout<<"\n y=";cin>>y; } inline float mean(sample s) { return float(s.x+s.y)/2.0; } void main() { sample x; char ch;

do { clrscr(); x.getdata(); cout<<"\n Mean value= "<<mean(x)<<"\n"; cout<<"\n"; cout<<"\n Want to continue(y/n):"; cin>>ch; }while(ch=='y'||ch=='Y'); } OUTPUT Enter two numbers to find the mean of them: x=45 y=67 Mean value= 56 Want to continue(y/n):Y Enter two numbers to find the mean of them: x=765 y=987 Mean value= 876 Want to continue(y/n):N

PRACTICAL NO 15

AIM: W.a.p to find greatest of two given numbers in two different classes using friend function.
SOURCE CODE: #include <iostream.h> #include <conio.h> #include <stdio.h> class big1; class big { int x; public: void getdata(); friend void find_big(big,big1); }; void big::getdata() { cout<<"\n Enter first number:\n"; cout<<"\n x=";cin>>x; }

class big1 { int y; public: void getdata(); friend void find_big(big,big1); }; void big1::getdata() { cout<<"\n Enter second number:\n"; cout<<"\n y=";cin>>y; } void find_big(big b,big1 b1) { if(b.x>b1.y) cout<<b.x<<" is bigger than "<<b1.y; else cout<<b1.y<<" is bigger than "<<b.x; } void main() { big B;big1 B1; char ch; do { clrscr(); B.getdata(); B1.getdata(); find_big(B,B1); cout<<"\n"; cout<<"\n Want to continue(y/n):"; cin>>ch; }while(ch=='y'||ch=='Y'); } OUTPUT Enter first number: x=908 Enter second number: y=6789 6789 is bigger than 908 Want to continue(y/n):Y Enter first number: x=12 Enter second number:

y=8765 8765 is bigger than 12 Want to continue(y/n):N

You might also like