Sei sulla pagina 1di 31

6.FUNCTION OVERLOADING a. AIM: To calculate the power value of the given numbers using classes with function overloading.

PROGRAM CODING: #include<iostream.h> #include<conio.h> class power { public: double calculate(float,int) long int calculate(int,int); void display(double); void display(long int); }; double power::calculate(float a,int n) { double p=1; for (int i=0;i<n;i++) p=p*a; return p; } long int power::calculate(int a,int n) { long int p=1; for(int i=0;i<n;i++) p=p*a; return p; } void power::display(double p) { cout<<the value of a:; cout<<p<<\n; } void power::display(long int p) { cout<<the value of b:<<b; } void main() { clrscr(); double c; int b,n; float a; long int d; cout<<enter the float value:;

cin>>a; cout<<enter the integer value:; cin>>n; power p; c=p.calculate(a,n); cout<<enter the integer value:; cin>>b; cout<<enter the integer value:; cin>>n; d=p.calculate(b,n); cout<<\n power value\n; p.display(c); p.display(d); getch(); }

b. AIM: To calculate the area of the square and rectangle using two classes with function overloading. PROGRAM CODING: #include<iostream.h> #include<conio.h> class a { int a,b; public: void area(int); void area(int ,int); void display(); }; class b { int x,y; public: void area(int); void area(int,int); void display(); }; void a::area(int a1) { a=a1*a1; } void a::area(int l,int b) { b=l*b; } void a::display() { cout<<area of the square:<<a; cout<<\n area of the rectangle:<<b; } void b::area(int a1) { x=a1*a1; } void b::area(int l,int b) { y=l*b; } void b::display() { cout<<area of the square:<<x; cout<<\n area of the rectangle:<<y;

} void main() { int s1,s2,l1,l2,b1,b2; cout<<enter the side:; cin>>s1; cout<<enter the length:; cin>>l1; cout<<enter the breadth:; cin>>b1; a a; a.area(s1); a.area(l1,b1); cout<<enter the side:; cin>>s2; cout<<enter the length:; cin>>l2; cout<<enter the breadth:; cin>>b2; b b; b.area(s2); b.area(l2,b2); cout<<\t class1\n; a.display(); cout<<\tclass2\n; b.display(); getch(); }

7. OPERATOR OVERLOADING INCLUDING UNARY & BINARY OPERATORS

1)USING MEMBER FUNCTION 2)USING FRIEND FUNCTION a. AIM: To increment the given values using operator overloading with unary operators.

Program Coding: #include<iostream.h> #include<conio.h> class increment { int a,b; char c; public: increment(); void operator++(); void display(); }; increment :: increment() { cout<<enter the value of a:; cin>>a; cout<<enter the value of b:; cin>>b; cout<<enter a character:; cin>>c; } void increment::operator++() { a++; b++; c++; } void increment::display() { cout<<the value of a:<<a<<\n; cout<<the value of b:<<b<<\n; cout<<the value of c:<<c; } void main() { clrscr(); increment inc; ++inc; inc.display(); getch(); }

b. AIM: To calculate the addition of two objects using operator overloading with binary operator. Program Coding: #include<iostream.h> #include<conio.h> class add { int a,b; public: add(int ,int); add(){} add operator+(add); void display(); }; add::add(int d,int e) { a=d; b=e; } add add::operator(add t ) { add temp; temp.a=a+t.a; temp.b=b+t.b; return temp; } void add::display() { cout<<\t The Addition Of Two Objects ; cout<<\nThe Value Of a:<<a; cout<<\n The value of b:<<b; } void main() { int c,d; clrscr(); cout<<enter the value of a:; cin>>c; cout<<enter the value of b:; cin>>d; add a1(c,d); cout<<enter the value of a:; cin>>c; cout<<enter the value of b:; cin>>d; add a2(c,d); add a3; a3=a1+a2; a3.display(); getch(); }

c. AIM: To increment the given values using friend function in operator overloading. Program Coding: #include<istream.h> #include<conio.h> Class increment { Int a; Float b; Char c; Public: Increment(){} Increment(int ,float,char); Friend void operator++(increment &); Void display(); }; Increment::increment(int f,float d,char e) { A=f; B=d; C=e; } Void operator++(increment & i1) { ++i1.a; ++i1.b; ++i1.c; } Void increment::display() { Cout<<\t The increment values\n); Cout<<\n The integer value a:<<a; Cout<<\n The float value b:<<b; Cout<<\n The character c:<<c; } Void main() { Int f; Float d; Char e; Clrscr(); Cout<<Enter the integer value:; Cin>>f; Cout<<Enter the float value:; Cin>>d; Cout<<Enter the character:; Cin>>e; Increment inc(f,d,e); ++inc; Inc.display(); Getch(); }

8. INHERITANCE b. Aim: To display the student details by using the multilevel inheritance. Program Coding: /*Multilevel Inheritance*/ #include<iostream.h> #include<conio.h> class person { public: char name[20]; int age; }; class student : public person { public: char dept[10],cn[50]; long int regno; }; class examination : public student { int marks[10],nos,total; float avg; public: void getcal(); void display(); }; void examination::getcal() { cout<<enter the name :; cin>>name; cout<<enter the age:; cin>>age; cout<<enter the department:; cin>>dept; cout<<enter the regno:; cin>>regno; cout<<enter the college name:; cin>>cn; cout<<enter the no of subjects:; cin>>nos; total=0; cout<<enter the <<nos<<subject marks:; for(int i=0;i<nos;i++) { cin>>marks[i]; total=total+marks; } avg=float(total)/nos; }

void examination :: display() { cout<<\tstudent details; cout<<\nname :<<name; cout<<\nregister no:<<regno; cout<<\nage :<<age; cout<<\ndepartment :<<dept; cout<<\ncollege :<<cn<<\n; cout<<\nno.of.subject:<<nos; cout<<\n<<nos<<subject marks:\n; for(int i=0;i<nos;i++) cout<<marks[i]<<\n; cout<<total :<<total<<\n; cout<<percentage :<<avg; } void main() { examination e; e.getcalc(); e.display(); getch(); }

c. Aim: To display the student details by using the multiple inheritance. Program Coding: /*Multiple Inheritance*/ #include<iostream.h> #include<conio.h> class student { protected: char name[20]; long int regno; char branch[20]; int m1,m2,m3; }; class sports { public: int games; }; class exam : public student, public sports { protected: int total float avg; public: void getcal(); void display(); }; int sports::games() { int ch; cout<<enter whether the student is in sports activities (or) not \n :; cout>>1.yes \n 2.no; cout<<enter your choice:; cin>>ch; return ch; } void exam :: getcalc() { cout<<enter the student name:; cin>>name; cout<<enter the regno:; cin>>regno; cout<<enter the branch:; cin>>branch; cout<<enter the 3 subject marks:; cin>>m1>>m2>>m3; int b; b=games(); if(b==1) { if(m1>=90) m1 =100;

else m1 = m1+10; if(m2>=90) m2 =100; else m2=m2+10; if(m3>=90) m3 =100; else m3=m3+10; } total=m1+m2+m3; avg=float(total)/3; } void exam :: display() { cout<<\tstudent details\n\t\t****************\n; cout<<\nname :<<name; cout<<\nregister no:<<regno; cout<<\n branch :<<branch; cout<<\n mark 1 :<<m1; cout<<\n mark 2 :<<m2<<\n; cout<<\n mark 3:<<m3; cout<<total :<<total<<\n; cout<<percentage :<<avg; } void main() { clrscr(); exam e; e.getcalc(); e.display(); getch(); }

d. AIM: To display the examination results of the students and also Display the employee details using hybrid inheritance. Program Code: /*Hybrid Inheritance*/ #include<iostream.h> #include<conio.h> class person { public: char name[20]; int age; }; class employee:public person { public: long int salary; char job[50]; void getdata1(); void display1(); }; class student : public person { public: long int regno; char branch[50]; int m1,m2,m3; void getdata2(); void display2(); }; class result : public employee, public student { int total; float avg; float netsalary , bonus; pulic: void calculate(); void display(); }; void employee :: getdata1() { cout<<enter the name :; cin>>name; cout<<enter the age:; cin>>age; cout<<enter the job:; cin>>job; cout<<enter the salary : cin>>salary; } void employee :: display1(); { cout<<name : <<name<<\n age :<<age; }

void student :: getdata2() { cout<<enter the name :; cin>>name; cout<<enter the age:; cin>>age; cout<<enter the regno:; cin>>regno; cout<<enter the branch : cin>>branch; cout<<enter the 3 subject marks:; cin>>m1>>m2>>m3; } void student :: display2() { cout<<name : <<name << \n age : <<age; } void result :: calculate() { total = m1+m2+m3; avg =float(total)/3; if(salary >= 50000) bonus = salary * 0.1; else if(salary >= 40000) bonus=salary*0.08; else if(salary >= 25000) bonus=salary*0.06; else if(salary >= 10000) bonus=salary*0.04; else bonus = salary * 0.2; netsalary=bonus+salary; } void result :: display() { cout<<\t employee details \n; display1(); cout<<job : << job << \n salary : << salary<<\n bonus : <<bonus; cout<<\n netsalary : <<netsalary; cout<<\n\t student details \n; display2(); cout<reg no : <<regno<<\n branch : << branch; cout<<\n mark1: m1 <<\n marks2 : << m2 << \n marks:<<m3; cout<<\n total: << total <<\n percentage :<<avg; } void main() { clrsccr(); result r; r.getdata1(); r.getdata2(); r.calculate(); r.display(); }

9. IMPLEMENTATION OF CALL BY VALUE- CALL BY ADDRESS AND CALL BY REFERENCE VARIABLE a. AIM: To implement the factorial of given numbers using call by value implementation. Program Coding: /*Call by value*/ #include<iostream.h> #include<conio.h> void main() { long int factorial(int); int n; long int f; clrscr(); cout<<enter the valur of n:; cin>>n; f=factorial(n); cout<<the factorial of n is <<f; getch(); } long int factorial(int n) { long int f=1; for(int i=1;i<n;i++) f=f*i; return f; }

/* Call by Address*/ b. AIM: To implement the factorial of given numbers using call by address implementation. Program Coding: /* Call by Address*/ #include<iostream.h> #include<conio.h> void main() { long int factorial(int *n); int n; long int f; clrcsr(); cout<<enter the value of n:; cin>>n; f=factorial(&n); cout<<the factorial of n is <<f; getch(); } long int factorial(int *n) { long int f=1; for(int i=1;i<=*n;i++) f=f*1; return f; }

/* Call by Reference variable*/ C. AIM: To find the area of the square using call by reference variable implementation. Program Coding: /* Call by Reference variable*/ #include<iostream.h> #include<conio.h> void main() { void area (int &n); int a=10; int &b=a; clrscr(); cout<<\t before calling the function\n; cout<<the normal variable a=<<a; cout<<the reference variable b= <<b; area (b); cout<<\t after calling the function \n; cout<<area of the square \n; cout<<the normal variable a= <<a; cout<<the reference variable b= <<b; getch(); } void area(int &n) { n=n*n; }

10.Virtual Base Class Aim: To add the given numbers using the virtual base class Program coding: #include<iostream.h> #include<conio.h> class A { int a,b; public: void getdata1() { cout<<enter the value of a&b:; cin>>a>>b; } void display1() { cout<<the addition of two number:; cout<<a=<<a<<\nb=<<b<<\na+b=<<a+b; } }; class B: public virtual A { int a,b; public: void getdata2() { cout<<enter the value of a & b:; cin>>a>>b; } void display2() { cout<<class b\n; cout<<a=<<a<<\nb=<<b<<\na+b=<<a+b; } }; class C: virtual public A

{ int a,b; public: void getdata3() { cout<<enter the value of a& b:; cin>>a>>b; } void display3() { cout<<class c; cout<<\n a= <<a<<\n b=<<b<<\na+b=<<a+b; } }; class print : public B,public C { }; void main() { clrscr(); print p; p.getdata(); B b1; b1.getdata2(); C c1; c1.getdata3(); cout<<class A; p.dispaly1(); b1.display2(); c1.display3(); getch(); }

11.Virtual Function Aim: To find the arithmetic operations such as addition ,subtraction , and multiplication of two given numbers using virtual function. Program Coding: #include<iostream.h> #include<conio.h> class A { public: int a,b; A() { cout<<enter the value of a&b:; cin>>a>>b; } virtual void display() { cout<<class A\n; cout<<a=<<a<<\nb=<<b<<\na+b=<<a+b; } }; class B: public A { public: void display() { cout<<class B\n; cout<<a=<<a<<\nb=<<b<<\na-b=<<a-b; } }; class C: public A { public: void display() { cout<<class C\n; cout<<a=<<a<<\nb=<<b<<\na*b=<<a*b; } }; void main() { clrscr(); A a1; A *p; p = &a1; p->display(); B b1; p = &b1; p->display(); C c1;

p =&c1; p->display(); getch(); }

12.TEMPLATES a. Aim: To find the maximum among three given integer and float numbers using function templates. Program Coding: /* Function Templates*/ #include<iostream.h> #include<conio.h> template <class S> S max (S a, S b, S c) { if(a>b && a>c) return a; else if (b>a&&b>c) return b; else return c; } void main() { int a,b,c,d; float e,f,g,h; clrscr(); cout<<enter the three integer values:\n; cin>>a>>b>>c; cout<<enter the three float values:\n; cin>>e>>f>>g; d=max(a,b,c); cout<<The maximum of three integer values :<<d; h = max(e,f,g); cout<<The maximum of three float values :<<h; getch(); }

b. Aim: To sort the given elements using the class templates Program coding: /* Class Templates */ #include<iostream.h> #include<conio.h> template<class S> class sorting { public: void sort(S x[],int n) { S temp; for(int i=0;i<n;i++) { for(int j=i ;j<n;j++) { if(x[i]>x[j]) { temp=x[i]; x[i]=x[j]; x[j]=temp; } } } cout<<The sorted elements are; for(i=0;i<n;i++) cout<<x[i]<<\n; } }; void main() { clrscr(); int a[10],n1,n2,n3; float b[10]; char c[10]; cout<<Enter the no of elements:; cin>>n1; cout<<:Enter the integer elements :; for(int i=0;i<n1;i++) cin>>a[i]; sorting<int > s1; s1.sort(a,n1); sorting<float>s2; cout<<Enter the no of elements:; cin>>n2; cout<<Enter the floating numbers:; for(i=0;i<n2;i++) cin>>b[i]; s2.sort(b,n2); sorting<char>s3;

cout<<Enter the no of elements:; cin>>n3; cout<<Enter the characters:; for(i=0;i<n3;i++) cin>>c[i]; s3.sort(c,n3); getch(); }

13.Sequential File Access a. Aim:

To create a c++ program for sequential access in file handling. Program Coding: #include<iostream.h> #include<conio.h> #include<fstream.h> class student { char name[20],branch[10]; int rollno,m1,m2,m3,tot; float avg ; public: student() { cout<<enter the name:; cin>>name; cout<<enter the branch:; cin>>branch; cout<<enter the rollno:; cin>>rollno; cout<<enter the three subject marks:; cin>>m1>>m2>>m3; avg=float(tot)/3; avg=float(tot)/3; } void display() { ofstream out(file,ios::app); out<<name<<\n; out<<branch<<\n; out<<rollno<<\n; out<<m1<< <<m2<< <<m3<<\n; out<<tot<<\n; out<<avg<<\n; } }; void main() { clrscr(); student s; s.display(); getch(); } b. Aim: To create a c++ program to read the contents from the file. Program coding:

#include<iostream.h> #include<conio.h> #include<fstream.h> class student { char name[20],branch[10]; int rollno,m1,m2,m3,tot; float per; public: void getdata() { ifstream in(file.cpp); //while(1) { in>>name; in>>branch; in>>rollno; in>>m1>>m2>>m3; in>>tot>>per; if(in.eof()) // break; display(); } } void display() { cout<<\n name: <<name; cout<<\nbranch: <<branch; cout<<\n rollno: <<rollno; cout<<\n mark1: <<m1; cout<<\n mark2: <<m2; cout<<\n mark3: <<m3; cout<<\ntotal: <<tot; cout<<\n percentage: <<per; } }; void main() { clrscr(); student s; s.getdata; getch(); }

14.Random File Access Aim : To create a c++ program for random access in file handling concept. Program Coding: #include<iostream.h> #include<conio.h> #include<fstream.h> class random { char c; public: void p rint() { fstream inout(file1.cpp,ios::in|ios::out); for(int i=0;i<5;i++) inout<<i; inout.seekp(1,ios::beg); inout<<a; inout.seekp(3,ios::cur); inout<<b; inout.seekg(0,ios::beg); inout>>c; cout<<c; } }; void main() { clrscr() random r; r.print(); getch(); }

// //

d. AIM: To display the account details using the hierarchical inheritance. Program Coding: /*Hierarchical Inheritance*/ #include<iostream.h> #include<conio.h> class account { public: char name[10],bname[50],branch[20]; long int accno; void getdata(); }; class savingacc: public account { long int bamt,damt,withdrawamt,balance; public: void getcalc1(); void display1(); void withdraw1(); void deposit1(); }; class currentacc : public account { long int bamt,damt,withdrawamt,balance; public: void getcalc2(); void withdraw2(); void deposit2(); void display2(); }; void account :: getdata() { cout<<enter the name:;

cin>>name; cout<<enter the account number :; cin>>accno; cout<<enter the name of the bank:; cin>>bname; cout<<enter the branch:<<\n;cin>>branch; } void savingacc::getcalc() { int ch; getdata(); cout<<enter the balance amount:; cin>>bamt; cout<<enter whether u withdraw (or) deposit the amount\n; cout<<1.withdraw the amount\n 2.deposit the amount; cout<<\n enter your choice; cin>>ch; if(ch==1) withdraw1(); else deposit1(); } void savingacc:: withdraw() { cout<<enter the withdraw amount:; cin>>withdrawamt; display1(); cout<<withdraw amount :<<withdrawamt; int t; balance = bamt; t=balance withdrawamt; if(balance > withdrawamt && withdrawamt <=25000) { if(t>=500) balance = t; else cout<<minimum balance should be as rs.500\n; } else if(balance < withdrawamt) cout<<your withdrawamt should be less than rs.25000\n;

cout<<current balance amount:<<balance; } void savingacc::deposit1() { cout<<enter the deposit amount:; cin>>damt; balance = bamt + damt; display1(); cout<<deposit amount :<<damt; cout<<current balance amount:<<balance; } void currentacc :: getcalc2() { cout<<enter the withdrawamt:; cin>>withdrawamt; display2(); cout<<withdraw amount :<<withdrawamt; int t; balance = bamt; t= balance bamt; if(balance > withdrawamt) { if( t > = 5000) balance = t; else cout<<minimum balance amount should be as rs.5000\n; } else cout<<you have minimum balance amount; cout<<balance amount :<<balance; } void currentacc::deposit2() { cout<<enter the deposit amount; cin>>damt; balance = damt + bamt; display2(); cout<<deposit amount : <<damt; cout<<current balance amount :<<balance; }

void savingacc :: display1() { cout<<\n\t saving account details\n: cout<<name : <<name; cout<<\n account no : <<accno; cout<<\n name of the bank:<<bname; cout<<\n branch : <<branch; cout<<initial balance amount :<<bamt; } void currentacc :: display2() { cout<<\n\t current account details\n: cout<<name : <<name; cout<<\n account no : <<accno; cout<<\n name of the bank:<<bname; cout<<\n branch of the bank: <<branch; cout<<initial balance amount :<<bamt; } void main() { clrscr savingacc sa; currentacc ca; cout<<type of account \n 1. saving acc \n 2. current acc; cout<<\n enter your choice:; int ch; cin>>ch; if(ch==1) sa.getcalc1(); else if(cn == 2) ca.getcalc2(); else cout<<enter the choice as 1 or 2; getch(); }

Potrebbero piacerti anche