0% found this document useful (0 votes)
21 views

1) #Include #Include Class Student (Char s1 (34) Int Roll Public

The document contains code snippets demonstrating various C++ concepts like classes, objects, methods, constructors, destructors, inheritance, polymorphism, operator overloading, etc. Specifically: 1. Defines a student class with data members and methods to get and display data. 2. Defines an employee class with data members and methods to read and display data. 3. Demonstrates friend functions and passing objects as parameters. 4. Demonstrates static data members and methods. 5. Defines an inline function. 6. Demonstrates constructor with parameters and copy constructor. 7. Demonstrates different constructors and destructor. 8. Shows inheritance and access specifiers for base

Uploaded by

Sanskar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

1) #Include #Include Class Student (Char s1 (34) Int Roll Public

The document contains code snippets demonstrating various C++ concepts like classes, objects, methods, constructors, destructors, inheritance, polymorphism, operator overloading, etc. Specifically: 1. Defines a student class with data members and methods to get and display data. 2. Defines an employee class with data members and methods to read and display data. 3. Demonstrates friend functions and passing objects as parameters. 4. Demonstrates static data members and methods. 5. Defines an inline function. 6. Demonstrates constructor with parameters and copy constructor. 7. Demonstrates different constructors and destructor. 8. Shows inheritance and access specifiers for base

Uploaded by

Sanskar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

1)

#include<iostream.h>
#include<conio.h>
class student{
char s1[34]; int roll;
public:
void getdata(){
cout<<"Enter Your First Name"; cin>>s1;
cout<<"Enter Your Rollno"; cin>>roll; }
void display(){
cout<<"Your Name is:::"<<s1<<endl;
cout<<"Your Rollno is:::"<<roll;}};
void main(){student p;
p.getdata();
p.display();
getch(); }
 
2)
#include<iostream>
#include<conio.h>
class employee{
int empno; char name[20]; char add [25];
public:
void read();
void display();
};
void employee::read(){
cout<<"Enter empno, name, address";
cin>>empno; cin>>name;cin>>add; }
void employee::display(){
cout<<"Empno:"<<empno<<"\t";
cout<<"Name:"<<name<<"\t";
cout<<"Address:"<<add;}
void main(){
employee t;
t.read();
t.display();
getch();}
3)
#include<iostream.h>
#include<conio.h>
class class2;// forward declaration for providing parameter class2 o2
class class1
{
int a;
public: void main()
void setvalue(int c)
{a=c; }
{class1 c1;
friend int max(class1 o1, class2 o2); class2 c2;
}; c1.setvalue(23);
class class2
{ c2.setvalue(32);
int b; cout<<"Maximum among
public:
void setvalue(int d)
them is: ";
{b=d; } cout<<max(c1,c2);getch();}
friend int max(class1 o1,class2 o2);
};
int max(class1 o1,class2 o2)
{return o1.a>o2.b?o1.a:o2.b;}
4. # include <iostream.h> void main()
class test {test t1,t2;
{int code; t1.setcode();
static int count; t2.setcode();
public: test::showcount();
void setcode(void) test t3;
{ t3.setcode();
code=++count; test::showcount();
} t1.showcode();
void showcode(void) t2.showcode();
{ t3.showcode();
cout<<"Object cin.get();}
number:"<<code<<endl;}
static void showcount(void)
{cout<<"\nCount: "<<count<<endl;}};
int test::count;

5.
#include<iostream.h>
inline int max(int a, int b)
{
return a > b ? a : b;
}
void main()
{
cout<<max(10,20);
cout<<" "<<max(99,88);
cin.get();
}
5
6) #include<iostream.h>
#include<conio.h>
#include<string.h>
class apple{
char name[34]; int amount;
public:
apple(){
name[0]='\0'; amount=10; }
apple(char c[], int r){
strcpy(name,c);amount=r; }
void display(){
cout<<"The name is:"<<name;
cout<<"\tAmount is:"<<amount<<endl;}
};
void main(){apple a;
apple aa("Marphali",4);
a.display();
aa.display();
getch();
}
7)
#include<iostream.h>
#include<conio.h>
class code{
int id;
public:
code(){}
code(int a){ id=a; }
code (code &x) {id=x.id; }
void display(){ cout<<id<<endl; }
~code()
{cout<<“Object Destroyed”<<endl;
getch(); } };
void main(){
code A(100);
code B(A);
code C=A;
code D;
D=A;
A.display();
B.display();
C.display();
D.display(); }
8.
#include<iostream.h>
#include<conio.h>
class Parent{
private: int three;
protected: int two;
public: int one;
Parent(){ one=1;two=2;three=3;}
void pdisplay(){
cout<<one<<two<<three<<endl;}
};
class child: public Parent{
public:void cdisplay(){
cout<<one<<two<<endl;} };
void main(){
Parent p;
child c;
p.pdisplay();
c.cdisplay();
cout<<c.one<<endl;
/*cout<<two; cout<<p.three; are not accessible they protected and private*/
getch(); }
9. #include<iostream.h>
#include<conio.h>
class student{
protected: int roll; char name[34];
public: void sget(){ cout<<"Enter roll & name:"; cin>>roll>>name;}
void sdisplay(){ cout<<"Your roll and name is:"<<roll<<“, "<<name<<"\t"; }
};
class test: virtual public student{
protected: int marks;
public: void tget(){ cout<<"Enter marks:"; cin>>marks; }
void tdisplay(){ cout<<"Your marks is:"<<marks<<"\t"; } };
class sports : virtual public student{
protected: int score;
public: void ssget(){ cout<<"Enter your score:"; cin>>score;}
void ssdisplay(){ cout<<"Your score is:"<<score<<"\t";} };
class result: public test, public sports{
int total;
public:void rget(){ sget(); tget(); ssget(); total=marks+score;}
void rdisplay(){ sdisplay(); tdisplay(); ssdisplay(); cout<<“Your total is:"<<total; }
};
void main(){
result r; r.rget(); r.rdisplay();
getch(); }
10.#include<iostream.h> {strcpy(qual,q); strcpy(dept,d);}
#include<conio.h> void display()
#include<string.h> {cout<<name<<","<<age<<","<<add<<","
class person <<qual<<","<<dept<<endl;}};
{protected: class student:public person
char name[15]; {private:
int age; char prog[5]; char sem[3];
char add[10]; public:
public: student(char n[],int a,char ad[],char p[],
char s[]):person(n,a,ad)
person(){}
{strcpy(prog,p); strcpy(sem,s);}
person(char n[],int a,char ad[])
void display()
{strcpy(name,n); age=a;
{cout<<name<<","<<age<<","<<add<<","
strcpy(add,ad); }}; <<prog<<","<<sem<<endl;}};
class teacher:public person void main()
{private: {teacher
char qual[10]; t("Ram",34,"PKR","Master","Math");
char dept[10]; student st("Hari",22,"KTM","S/W","II");
public: t.display();
teacher(char n[],int a,char ad[],char q[], st.display();
char d[]):person(n,a,ad) getch(); }
11) #include<iostream.h>
#include<conio.h>
class truck{
public: virtual void display(){
cout<<"I am from truck class\n";}
};
class bus{
truck t;
public:void display(){
cout<<"I am from bus class\n";
t.display();} };
void main(){
bus *b; bus b;
b=new bus; b.display(); Compile time
RUNTIME
b->display(); getch();} polymorphism
polymorphism
getch(); }  
12)
#include<iostream.h>
#include<conio.h>
class unary{
int p;int q;int r;
public:
void getdata(int a,int b,int c);
void display();
void operator-(); };
void unary:: getdata(int a,int b,int c){ p=a;q=b;r=c;}
void unary::display(){ cout<<"p is:"<<p<<"\tq is:"<<q<<"\tris:"<<r;}
void unary::operator-(){ p=-p;q=-q;r=-r; }
void main(){ clrscr();
unary u; u.getdata(30,-50,40);
cout<<"Before overloaded\t";
u.display();
cout<<"\nAfter overloaded\t";
-u;//overloading -operator
u.display();
getch(); }
13. #include<conio.h>
#include<iostream.h>
class distance{
private: int feet;float inches;
public:
distance() {}
distance(int a,float b){ feet=a; inches=b;
while(inches>=12){ feet++;inches-=12;} }
void showdist(){ cout<< feet <<“ft”<<"\t"<<inches<<“inch“<<endl;}
distance operator+(distance); };
distance distance::operator+(distance d){
distance d1; d1.feet=feet+d.feet; d1.inches=inches+d.inches;
if( d1.inches>=12.0){ d1.inches-=12.0;d1.feet++; } return d1; }
void main(){
distance dist1(8,14 );
distance dist2(3,27 );
distance dist3;
dist3=dist1+dist2;//operator overloading
dist1.showdist();
dist2.showdist();
dist3.showdist();
getch(); }
14. #include<iostream.h> int main()
#include<conio.h> {
class player player pl[3];
{ public: for(int i=0; i<3; i++)
int pno,gno,ano;
public: {
player() pl[i];
{cout<<"\nEnter pno,gno and ano of }
players:"; player greatest=pl[0];
cin>>pno>>gno>>ano; } for(int i=0; i<3; i++)
int tot(){return gno+ano;} {
void display() if(pl[i].tot()>greatest.tot())
{cout<<pno<<" "<<gno<<" “ {
<<ano<<endl; } greatest=pl[i];
player operator >(player p) }
{return(this->tot()>p.tot()?*this:p);} }
}; greatest.display();
getch(); return 0; }
15#include<iostream.h>
#include<conio.h>
class memory{
int byte, mb,kb;
public: memory(int b){
mb=b/1048576; //(1024*1024)
int fkb=b%1048576;
kb=fkb/1024;
byte=fkb%1024;}
void display(){
cout<<"The mb:"<<mb;
cout<<"\tThe kb:"<<kb;
cout<<"\tThe byte:"<<byte;} };
void main(){ int k;
cout<< “Enter integer bytes”; cin >>k;
memory m=k;
m.display();
getch();}
16. #include<iostream.h>
#include<conio.h>
const float m=3.280833;
class distance{ int feet; float inches;
public: distance(){ feet=0; inches =0;}
distance(float meter){
float ft= m*meter; feet=int(ft);inches= 12*(ft-feet); }
distance(int f, float i){ feet=f, inches =i;}
void show(){ cout<< feet<<"\t"<<inches;}
operator float(){ float fractfeet=inches/12;
fractfeet+=float(feet);
return fractfeet/m; } };
void main(){
distance d=1;//meter
cout<<"Distance 1 Meter is :"; d.show(); d=2.35;
cout<<"\nDistance is 2.35 M:"; d.show();
distance d2(10,10.25);
float met=float(d2); cout<<"\nDistance is(10 F,10.25 I):";
cout<<met<<”Meter”;
getch();}
17. #include<iostream.h>
#include<conio.h>
class base{
public:
void display(){ cout<<"\tDisplay base class";}
virtual void show(){ cout<<"\n\tShow base class";} };
class derived: public base{
public:
void display(){ cout<<"\tDisplay derived class";}
void show(){ cout<<"\n\tShow derived class";} };
void main(){
base b; derived d;
base*bpt;
cout<<"\nPointer to base:\n"; bpt=&b;
bpt->display(); bpt-> show();
cout<<"\nPointer to derived class :\n "; bpt=&d;
bpt-> display(); bpt-> show();
getch(); }
18. #include<iostream.h>
#include<conio.h>
class average{
private: int a,b;
public: void setvalue(){ a=21,b=48; }
friend float mean(average av);
};
float mean(average av) { return
float(av.a+av.b)/2.0;}
void main(){
average avg;
avg.setvalue();
cout<<"Final average is:";
cout<<mean(avg);
getch(); }
19.
#include<iostream.h>
#include<conio.h>
template<class T1,class T2>
class Test{
T1 a; T2 b;
public:
Test(T1 x,T2 y) { a=x; b=y; }
void show(){
cout<<a<<"\tand\t"<<b<<"\n";}
};
int main(){
Test<float,int>test1(1.23,123);
Test<int,char>test2(100,'W');
test1.show();
test2.show();
getch(); return 0; }
20.
#include<iostream.h>
#include<conio.h>
class pu void main(){
pu p;
{char faculty[30]; int top; p.push('H');
public: p.push('E');
p.push('L');
pu(){top=-1;} p.push('L');
p.push('O');
void push( char vari) cout<<p.pop();
cout<<p.pop();
{ faculty[++top]=vari;} cout<<p.pop();
char pop(){ return cout<<p.pop();
cout<<p.pop();
faculty[top--];} getch();}

};
21. #include<iostream.h>
#include<conio.h>
template<class T>
void bubble(T a[],int n){
for (int i=0;i<n-1;i++)
for(int j=n-1;i<j;j--)
if(a[j]<a[j-1]){swap(a[j],a[j-1]);}
}
template <class T>
void swap(T &x,T &y){ T temp=x; x=y;y=temp;}
int main(){
int x[5]={10,50,30,40,20};
float y[5]={1.1,2.3,4.2,2.5,6.5};
bubble(x,5); bubble(y,5);
cout<<"Sorted x=";
for(int i=0;i<5;i++) cout<<x[i]<<“, "; cout<<endl;
cout<<"Sorted Y=";
for(int j=0;j<5;j++)
cout<<y[j]<<", "; cout<<endl;
getch(); return 0;}
22(Multiple exception)#include<iostream>
#include<conio.h>
int main(){ float Op1, Op2, Result; char Opr;
try {
cout <<"To proceed, enter a number, an operator, and a number:\n";
cin >> Op1 >> Opr >> Op2;
if(Opr != '+'&& Opr != '-'&& Opr != '*'&& Opr != '/')
throw Opr;
switch(Opr) {
case'+': Result = Op1 + Op2; break;
case'-': Result = Op1 - Op2;break;
case'*': Result = Op1 * Op2; break;
case'/': Result = Op1 / Op2; break;}
cout <<"\n"<< Op1 <<" "<< Opr <<" "<< Op2 <<" = "<< Result; }
catch(const char n) { cout <<"\nOperation Error: "<< n <<" is not a valid
operator"; }
getch(); return 0; }
23. #include<iostream.h>
#include<vector>
#include<conio.h>
using namespace std; //using std:: vector;
void main(){
vector<char>v;
v.push_back('A'); v.push_back('B'); v.push_back('C');
v.push_back('D'); v.push_back('E'); v.push_back('F');
cout<<"Forward printing\t";
vector<char>:: iterator p;
for(p=v.begin();p!=v.end();p++)
cout<<*p<<","; cout<<endl;
cout<<"Reversed printing\t";
vector<char>:: reverse_iterator rp;
for(rp=v.rbegin(); rp!=v.rend();rp++)
cout<<*rp<<","; cout<<endl; getch(); }
Thanks
Best of LUCK
Be prepare for Lab
Practical
(VIVA, CODE, Lab copy)

You might also like