Você está na página 1de 26

PROGRAM NO 9

WAP to print Fibonacci series using copy constructor.

#include<iostream.h> #include<conio.h> class fibo {int a,b,c; public: fibo() {a=0; b=1; cout<<a<<endl; cout<<b<<endl; } fibo(fibo &f) {f.c=f.a+f.b; cout<<f.c<<endl; f.a=f.b; f.b=f.c;} };

void main() {clrscr(); int n; cout<<"Enter the numebr of terms: ";

cin>>n; fibo f; for(int i=0;i<(n-2);i++) {fibo f2(f);} getch(); }

OUTPUT

PROGRAM NO 10
WAP to add 2 complex numbers using constructor overloading.

#include<iostream.h> #include<conio.h> class complex {int m,n; public: complex(){} complex(int x) {m=n=x;} complex(int x,int y) {m=x; n=y;} friend complex sum(complex, complex); void show() {cout<<m<<" + i"<<n;} };

complex sum(complex c1,complex c2) {complex c3; c3.m=c1.m+c2.m;

c3.n=c1.n+c2.n; return c3;} void main() {clrscr(); int x,y; cout<<"Enter the real and imaginary values of complex 1: "<<endl; cin>>x>>y; complex c1(x); complex c2(x,y); cout<<"Enter the real and imaginary values of complex 2: "<<endl; cin>>x>>y; complex c3(x); complex c4(x,y); complex c5=sum(c1,c3); cout<<"The sum of 2 complex number when only 1 value are passed is: "; c5.show(); c5=sum(c2,c4); cout<<endl<<"The sum of 2 complex number when 2 values are passed are: "; c5.show(); getch(); }

PROGRAM NO 11
WAP to demonstrate the use of this pointer.

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

class pointer {int data; public: pointer() {} pointer(int d) {data=d;} void display() {cout<<data;} pointer& operator=(pointer& p) {data=p.data; //Not done automatically cout<<endl<<"Assignment Operator invoked."; return *this; //Return copy of this pointer } };

void main() {clrscr(); pointer p1(37);

pointer p2,p3; p3=p2=p1; //Invoke overloaded = twice cout<<endl<<"a2= "; p2.display(); cout<<endl<<"a3= "; p3.display(); getch(); }

PROGRAM NO 12
Create a base class basic_info with data members name ,roll no, sex and two member functions getdata and display. Derive a class physical_fit from basic_info which has data members height and weight and member functions getdata and display. Display all the information using object of derived class.

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

class basic_info {int rollno; char name[25],sex; public: void getdata_bi() {cout<<"Enter name: "; gets(name); cout<<"Enter roll number: "; cin>>rollno; cout<<"Enter sex: "; cin>>sex; } void display_bi() {cout<<endl<<"Name: "<<name; cout<<endl<<"Roll Number: "<<rollno; cout<<endl<<"Sex:"<<sex;

} };

class physical_fit:public basic_info {int height,weight; public: void getdata_pf() {cout<<"Enter height: "; cin>>height; cout<<"Enter weight: "; cin>>weight; } void display_pf() {cout<<endl<<"Height: "<<height; cout<<endl<<"Weight: "<<weight; } };

void main() {clrscr(); physical_fit pf; pf.getdata_bi(); pf.getdata_pf(); pf.display_bi();

pf.display_pf(); getch(); }

PROGRAM NO 13
Design three classes STUDENT ,EXAM and RESULT. The STUDENT class has data members such as rollno, name. create a class EXAM by inheriting the STUDENT class. The EXAM class adds data members representing the marks scored in six subjects. Derive the RESULT from the EXAM class and has its own data members such as total marks. Write a program to model this relationship.

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

class student {protected: int rollno; char name[25]; public: void getdata_s() {cout<<"Enter name: "; gets(name); cout<<"Enter roll number: "; cin>>rollno; } void display_s() {cout<<endl<<"Name: "<<name; cout<<endl<<"Roll Number: "<<rollno;

} };

class exam:public student {protected: int m[6]; public: void getdata_e() {cout<<"Enter marks in 6 subjects: "<<endl; for(int i=0;i<=5;i++) {cin>>m[i];} } void display_e() {cout<<endl<<"Marks in 6 subjects are: "; for(int i=0;i<=5;i++) {cout<<endl<<m[i];} } };

class result:public exam {int ttl; public: result() {ttl=0;} void ttlmrks()

{for(int i=0;i<=5;i++) {ttl=ttl+m[i];} cout<<endl<<endl<<"Total marks is: "<<ttl; } };

void main() {clrscr(); result r; r.getdata_s(); r.getdata_e(); r.display_s(); r.display_e(); r.ttlmrks(); getch(); }

PROGRAM NO 14
Write a program to overload assignment (=) operator.

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> class equal { char a[45]; public: void getdata() {cout<<"Enter string: "; gets(a); } void display() {cout<<"The string is: "<<a;} void operator=(equal e) {strcpy(e.a,a); cout<<endl<<"Now string is: "<<e.a; } };

void main() {

clrscr(); equal e1,e2; e1.getdata(); e2.getdata(); cout<<"\n"; e1.display(); cout<<"\n"; e2.display(); e1=(e2); getch(); }

PROGRAM NO 15
Write a program to overload binary + operator.

#include<iostream.h> #include<conio.h> #include<stdio.h> class plus { int a,b; public: void getdata() {cout<<"Enter real part of complex number: "; cin>>a; cout<<"Enter imaginary part of complex number: "; cin>>b; } void display() {cout<<"Complex number is: "<<a; cout<<" + i"<<b<<endl; }

plus operator+(plus p) {plus p1; p1.a=a+p.a; p1.b=b+p.b;

return(p1); } };

void main() { clrscr(); plus p2,p3,p4; p2.getdata(); p3.getdata(); p4=p2+(p3); p2.display(); p3.display(); p4.display(); getch(); }

PROGRAM NO 16
Write a program to define the function template for calculating the square of given numbers with different data types.

#include<iostream.h> #include<conio.h> template <class t> void square(t a) { t b; b=a*a; cout<<"\nSquare of "<<a<<" is: "<<b; } void main() {clrscr(); int a; float b; cout<<"\nEnter the integer value: "; cin>>a; square(a); cout<<"\n\nEnter floating point value: "; cin>>b; square(b); getch(); }

PROGRAM NO 17
Write a program to illustrate how to define and declare a class template for reading two data items from the keyboard and to find their sum.

#include<iostream.h> #include<stdio.h> #include<conio.h> template<class t> class add { t a; t b; public: void get() {cout<<"\nEnter the values of a and b: "<<endl; cin>>a>>b; } void sum() {t sum=0; sum=a+b; cout<<"Sum of two no.s is: "<<sum<<endl; } }; void main() { clrscr();

add<int> ob; add<float> ob2; cout<<"\nInteger data: "; ob.get(); ob.sum(); cout<<"\nFloat data: "; ob2.get(); ob2.sum(); getch(); }

PROGRAM NO 18
Write a program to read a text file and display its contents on the screen.

#include<iostream.h> #include<fstream.h> #include<conio.h> void main () {clrscr(); char line[100]; ifstream getfile ("example.txt"); while(!getfile.eof()) {getfile.getline(line,100); cout<<line<<endl; } getfile.close(); getch(); }

PROGRAM NO 19

Write a Program to copy the contents of a file into another.

#include <fstream.h> #include <conio.h> #include <stdio.h>

int main() {clrscr(); char ch; char fname[20]; cout<<"Enter the name of the files whose contents are to be copied: "; gets(fname); fstream fl1(fname,ios::in); if(!fl1) {cout<<endl<<endl<<"E R R O R"; cout<<endl<<"File doesn't exists, now program will be terminated."; } else {cout<<endl<<"Enter the name of the file in which you want contents to be copied: "; gets(fname); fstream fl2(fname,ios::app); while(!fl1.eof()) {ch=fl1.get();

fl2<<ch; } fl1.close(); fl2.close(); cout<<endl<<"Copying Succesful."; } getch(); return 0; }

PROGRAM NO 20
Write a program to convert lowercase character to an uppercase character.

#include <fstream.h> #include <conio.h> #include <stdio.h> #include <ctype.h>

int main() {clrscr(); char ch; char fname[20]; cout<<"Enter the name of the files whose contents are to be copied: "; gets(fname); fstream fl1(fname,ios::in); if(!fl1) {cout<<endl<<endl<<"E R R O R"; cout<<endl<<"File doesn't exists, now program will be terminated."; } else {cout<<endl<<"Enter the name of the file in which you want the conversions: "; gets(fname); fstream fl2(fname,ios::out); while(!fl1.eof()) {ch=fl1.get();

ch=toupper(ch); // changing the case of the charachter. fl2<<ch; } fl1.close(); fl2.close(); cout<<endl<<"Conversion Succesful."; } getch(); return 0; }

Você também pode gostar