Sie sind auf Seite 1von 50

Q- WAP to create a class student _info which has the data members as name,rollno,percentage and sex.

Create function inputdata and showdata to get the values from the user & display them on the screen. SOL: - #include<iostream.h>
#include<conio.h> #include<stdio.h> class studentinfo { private: int rollno,per; char name[10],sex[4]; public: void inputdata() { cout<<"Enter the rollno,percentage,name and sex of the student:"; cin>>rollno>>per; gets(name); gets(sex); } void showdata() { cout<<"Name="<<name; cout<<"Rollno="<<rollno; cout<<"Percentage="<<per;

cout<<"Sex="<<sex; } }; void main() { studentinfo s; s.inputdata(); s.showdata(); getch(); }

OUTPUT:
Enter the rollno,percentage,name and sex of the student:101 80 ram male Name=ram Rollno=101 Percentage=80 Sex=male

Q- WAP when data members are defined outside the class.

SOL: - #include<iostream.h>
#include<conio.h> #include<stdio.h> class studentinfo { private: int rollno,per; char name[10],sex[9]; public: void inputdata(); void showdata(); }; void studentinfo::inputdata() { cout<<"Enter the rollno,percentage,name and sex of the student:"; cin>>rollno>>per; gets(name); gets(sex); } void studentinfo::showdata() { cout<<"Name="<<name; cout<<"Rollno="<<rollno; cout<<"Percentage="<<per; cout<<"Sex="<<sex;

} void main() { studentinfo s; s.inputdata(); s.showdata(); getch(); }

OUTPUT:
Enter the rollno,percentage,name and sex of the student:101 90 sham male Name=sham Rollno=101 Percentage=90 Sex=male

Q- WAP with the help of constructor. SOL: - #include<iostream.h>

#include<conio.h> #include<stdio.h> class student { private: int rollno; float marks; public: student() { rollno=0; marks=0.0; cout<<"\n Default constructor data rollno="<<rollno<<"Marks="<<marks; } student(int r,float m) { rollno=r; marks=m;cout<<"\n Parameterized constructor data Rollno="<<rollno<<"Marks="<<marks; } }; void main() { clrscr(); student obj;

student obj1(10,20); student obj2=student(100,30.5); getch(); }

OUTPUT:
Default constructor data rollno=0Marks=0 Parameterized constructor data Rollno=10Marks=20 Parameterized constructor data Rollno=100Marks=30.5

Q- WAP with the help of destructor. SOL: - #include<iostream.h>

#include<conio.h> #include<stdio.h> class student { private: int rollno; float marks; public: student() { rollno=0; marks=0.0; cout<<"\n Default constructor data rollno="<<rollno<<"Marks="<<marks; } student(int r,float m) { rollno=r; marks=m;cout<<"\n Parameterized constructor data Rollno="<<rollno<<"Marks="<<marks; } student(student&address) { rollno=address.rollno; marks=address.marks; cout<<"\n copy constructor data Rollno="<<rollno<<"Marks="<<marks;

} ~student() { cout<<"Memory destroyed"; } }; void main() { clrscr(); student obj; student obj1(10,20); student obj2=student(100,30.5); student obj3=obj2; getch(); }

OUTPUT:
Default constructor data rollno=0Marks=0 Parameterized constructor data Rollno=10Marks=20 Parameterized constructor data Rollno=100Marks=30.5 copy constructor data Rollno=100Marks=30.5Memory destroyedMemory destroyedMemor y destroyedMemory destroyed

Q- WAP to show inheritance. SOL: - #include<iostream.h>

#include<conio.h> #include<stdio.h> class basic_info { int rollno; char name[30]; public: void getdata(); void display(); }; class physical_fit:public basic_info { private: float height,weight; public: void getdata(); void display(); }; void basic_info::getdata() { cout<<"Enter the name nad rollno:"; gets(name); cin>>rollno; } void basic_info::display()

{ cout<<"Name="<<name; cout<<"Rollno="<<rollno; } void physical_fit::getdata() { basic_info::getdata(); cout<<"Enter the height and weight:"; cin>>height>>weight; } void physical_fit::display() { basic_info::display(); cout<<"Heught="<<height<<"Weight="<<weight; } void main() { clrscr(); physical_fit p; p.getdata(); p.display(); getch(); }

OUTPUT:
Enter the name nad rollno:manish

101 Enter the height and weight:162 60 Name=manishRollno=101Heught=162Weight=60

Q- WAP to implement hybrid inheritance with the base class as basic_info and acad_info and from acad_info a new class is arrived class result. SOL: - #include<iostream.h>
#include<conio.h> #include<stdio.h>

class basic_info { int rollno; char name[30]; public: void getdata(); void display(); }; class physical_fit:public basic_info { private: float height,weight; public: void getdata(); void display(); }; class acad_info:public basic_info { private: char sem[20],course[10]; public: void getdata(); void display(); }; class marks:public physical_fit,acad_info

{ float m1,m2,m3; public: void getdata(); void display(); }; void basic_info::getdata() { cout<<"Enter the name nad rollno"; gets(name); cin>>rollno; } void basic_info::display() { cout<<"Name="<<name; cout<<"Rollno="<<rollno; } void physical_fit::getdata() { basic_info::getdata(); cout<<"Enter the height and weight:"; cin>>height>>weight; } void physical_fit::display() {

basic_info::display(); cout<<"Heught="<<height<<"Weight="<<weight; } void acad_info::getdata() { cout<<"Enter the semester and course:"; gets(sem); gets(course); } void acad_info::display() { cout<<"Semester="<<sem<<"Course="<<course; } void marks::getdata() { physical_fit::getdata(); acad_info::getdata(); cout<<"Enter the marks of three subjects:"; cin>>m1>>m2>>m3; } void marks::display() { physical_fit::display(); acad_info::display(); cout<<"Marks of 1st subject"<<m1;

cout<<"Marks of 2nd subject"<<m2; cout<<"Marks of 3rd subject"<<m3; } void main() { clrscr(); marks k; k.getdata(); k.display(); getch(); }

OUTPUT:
Enter the name nad rollnosavita 123 Enter the height and weight:160 50 Enter the semester and course:second bba(cam) Enter the marks of three subjects:60 77 80 Name=savitaRollno=123Heught=160Weight=50Semester=secondCourse=b ba(cam)Marks of 1 st subject60Marks of 2nd subject77Marks of 3rd subject80

Q- WAP to create a file using a constructor. SOL: - #include<iostream.h>


#include<conio.h> #include<stdio.h> #include<fstream.h> void main() { char name[25]; float rollno;

cout<<"Enter the name and rollno:"; gets(name); cin>>rollno; ofstream out("Student"); out<<name; out<<"\n"; out<<rollno; out.close(); ifstream in("student"); in>>name; in>>rollno; cout<<"Name="<<name<<"Rollno="<<rollno; in.close(); getch(); }

OUTPUT:
Enter the name and rollno:Sweety 157 Name=SweetyRollno=157

Q- WAP to create a file payroll and write the following data in the file employee number,name and salary using a constructor.Display the data written in a file on screen. SOL: - #include<iostream.h>
#include<conio.h> #include<stdio.h> #include<fstream.h> void main() { clrscr(); char e_name[25];

int sal,e_num; cout<<"Enter the employee number,name and salary:"; cin>>e_num; gets(e_name); cin>>sal; ofstream out("Payroll"); out<<e_num; out<<"\n"; out<<e_name; out<<"\n"; out<<sal; out.close(); ifstream in("Payroll"); in>>e_num; in>>e_name>>sal; cout<<"Employee number="<<e_num<<"\n"<<"Employee name="<<e_name<<"\n"<<"salary="<<sal; in.close(); getch(); }

OUTPUT:
Enter the employee number,name and salary:1568 Ranjeet 10000 Employee number=1568

Employee name=Ranjeet salary=10000

Data written in a file:


1568 Ranjeet 10000

Q- WAP to show the concept of default argument for simple intrest. SOL: - #include<iostream.h>
#include<conio.h> void si(float p,float t,float r=1.5); void main() { float p,t; cout<<"Enter the value of p and t"; cin>>p>>t; si(p,t); getch(); }

void si(float x,float y,float z) { float s; s=(x*y*z)/100; cout<<s; }

OUTPUT:
Enter the value of p and t1000 1 15

Q- WAP to create a friend function for display a class student. SOL: - #include<iostream.h>
#include<conio.h> #include<stdio.h> class student { int rollno; char name[20]; public: void getdata(); friend void display(class student) };

void student::getdata() { cout<<"Enter name and rollno:"; gets(name); cin>>rollno; } void display(class student s) { cout<<"Name="<<s.name<<"Rollno="<<s.rollno; } void main() { student obj; obj.getdata(); display(obj); getch(); }

OUTPUT:
Enter name and rollno:Manjula 145 Name=ManjulaRollno=145

Q- WAP to create a class and show the use of static data members. SOL: - #include<iostream.h>
#include<conio.h> #include<stdio.h> class inventory { static int count; int no; public: void getdata(int a) { no=a; count++;

} void display() { cout<<"Count="<<count; } }; int inventory::count; void main() { clrscr(); inventory obj1,obj2,obj3; obj1.display(); obj2.display(); obj3.display(); obj1.getdata(100); obj2.getdata(200); obj3.getdata(300); obj1.display(); obj2.display(); obj3.display(); getch(); }

OUTPUT:
Count=0Count=0Count=0Count=3Count=3Count=3

Q- WAP to create a class and show the use of static member function. SOL: - #include<iostream.h>
#include<conio.h> #include<stdio.h> class test { static int count; int code; public: void setcode() { code=++count; } void showcode()

{ cout<<"Code="<<code; } static void display() { cout<<"Object member="<<count; } }; int test::count; void main() { clrscr(); test t1,t2; t1.setcode(); t2.setcode(); test::display(); test t3; t3.setcode(); test::display(); t1.showcode(); t2.showcode(); t3.showcode(); getch(); }

OUTPUT:

Object member=2Object member=3Code=1Code=2Code=3

Q- WAP to create a class using ambiguity multiple inheritance by virtual base class. SOL: - #include<iostream.h>
#include<conio.h> #include<stdio.h> class base1 { public: int a; void display() { cout<<a; } }; class d1:virtual public base1 {

public: int b; void display() { cout<<b; } }; class d2:virtual public base1 { public: int c; void display() { cout<<c; } }; class d:public d1,public d2 { public: int total; void display() { cout<<total; } };

void main() { clrscr(); d obj; obj.a=10; obj.b=20; obj.c=30; obj.total=obj.a+obj.b+obj.c; obj.display(); getch(); }

OUTPUT:
60

Q- WAP to show input,output operations on binary files. SOL: - #include<fstream.h>


#include<iostream.h> #include<conio.h> void main() { clrscr(); int num1,num2; num1=10; num2=5; fstream f; f.open("Data",ios::in/ios::out/ios::binary); f.write((char*)&num1,sizeof(num1)); f.write((char*)&num2,sizeof(num2)); f.read((char*)&num1,sizeof(num1)); cout<<num1<<"\t"<<num2; f.close(); getch(); }

OUTPUT:
10 5

Q- WAP to show the use of polymorphism. SOL: - #include<iostream.h>


#include<conio.h> #include<stdio.h> class student { int rollno; float marks; public: void getdata(int a,float b) { rollno=a; marks=b; } void display() { cout<<"Rollno="<<rollno; cout<<"Marks="<<marks; }

}; void main() { student s; student *ptr=&s; ptr->getdata(10,20); ptr->display(); getch(); }

OUTPUT:
Rollno=10Marks=20

Q- WAP to show the use static binding. SOL: - #include<iostream.h>


#include<conio.h> #include<stdio.h> class student { int rollno; float marks; public: void getdata(int a,float b) { rollno=a; marks=b; } void display() { cout<<"Rollno="<<rollno; cout<<"Marks="<<marks; } }; class physical_info:public student

{ float height,weight; public: void getdata(float a,float b) { height=a; weight=b; } void display() { cout<<"Height="<<height; cout<<"Weight="<<weight; } }; void main() { student s; student *ptr=&s; ptr->getdata(10,20); ptr->display(); physical_info p; physical_info *ptrd=&p; ptrd->getdata(5,10); ptrd->display(); getch(); }

OUTPUT:
Rollno=10Marks=20Height=5Weight=10

Q- WAP to overload a uniary operator ++ in c++. SOL: - #include<iostream.h>


#include<conio.h> #include<stdio.h> class ov { int num; public: ov(int m) { num=m; } void operator ++() { ++num; } void display() { cout<<num<<"\t"; } }; void main() {

ov obj(10); obj.display(); ++obj; obj.display(); getch(); }

OUTPUT:
10 11

Q- WAP to overload a uniary operator -- in c++. SOL: - #include<iostream.h>


#include<conio.h> #include<stdio.h> class ov { int num; public: ov(int m) { num=m; } void operator --() { --num; } void display() { cout<<num<<"\t"; } }; void main() { ov obj(10); obj.display();

--obj; obj.display(); getch(); }

OUTPUT:
10 9

Q- WAP to overload a uniary + in c++. SOL: - #include<iostream.h>

#include<conio.h> #include<stdio.h> class ov { int num; public: ov(int m) { num=m; } void operator +() { num=+num; } void display() { cout<<num<<"\t"; } }; void main() { clrscr(); ov obj(10); obj.display(); +obj;

obj.display(); getch(); }

OUTPUT:
10 10

Q- WAP to overload a uniary - in c++. SOL: - #include<iostream.h>


#include<conio.h>

#include<stdio.h> class ov { int num; public: ov(int m) { num=m; } void operator -() { num=-num; } void display() { cout<<num<<"\t"; } }; void main() { ov obj(10); obj.display(); -obj; obj.display(); getch();

OUTPUT:
10 -10

Q- WAP to overload a uniary operator - using friend function. SOL: - #include<iostream.h>


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

class ov { int num; public: ov(int m) { num=m; } friend void operator -(ov&obj1) { obj1.num=-obj1.num; } void display() { cout<<num<<"\t"; } }; void main() { clrscr(); ov obj(10); obj.display(); -obj; obj.display(); getch();

OUTPUT:
10 -10

Q- WAP to overload a uniary operator + using friend function. SOL: - #include<iostream.h>


#include<conio.h> class ov {

int num; public: ov(int m); friend void operator +(ov &obj1,ov &obj2); }; ov::ov(int m) { num=m; } void operator +(ov &obj1,ov &obj2) { ov obj3(0); obj3.num=obj1.num+obj2.num; cout<<"Obj1 number="<<obj1.num; cout<<"Obj2 number="<<obj2.num; cout<<"Obj3 number="<<obj3.num; } void main() { ov obj1(11),obj2(13); obj1+obj2; getch(); }

OUTPUT:

Obj1 number=11Obj2 number=13Obj3 number=24

Q- WAP to convert basic type to user defined type. SOL: - #include<iostream.h>


#include<conio.h> class meter { private: float length;

public: meter() { length=0.0; } meter(float l) { length=l/100.0; } void showlength() { cout<<"Length(in mtr)="<<length; } }; void main() { clrscr(); meter obj; float length; cout<<"Enter the length(in cm):"; cin>>length; obj=length; obj.showlength(); getch(); }

OUTPUT:
Enter the length(in cm):160 Length(in mtr)=1.6

Q- WAP to convert user defined type to basic type. SOL: - #include<iostream.h>


#include<conio.h> class meter { private: float length; public:

meter() { length=0.0; } operator float() { float lengthcms; lengthcms=length*100.0; return(lengthcms); } void getlength() { cout<<"Enter the length(in mtr):"; cin>>length; } }; void main() { clrscr(); meter meter1; float length1; meter1.getlength(); length1=meter1; cout<<"Length in(cms)="<<length1; getch();

OUTPUT:
Enter the length(in mtr):1.6 Length in(cms)=160

Das könnte Ihnen auch gefallen