You are on page 1of 23

APSE LAB

Submitted by:

Ravinder Kumar
Roll no: 1120021
Section: E1
Submitted to:
Dr.
Assistant Professor

Electrical Engineering Department


National Institute of Technology Kurukshetra
August, 2015

Page | 1

SN

EXPERMINENT NAME

PAGE NO

Implementation of class and member function


by student name , class and function for grade

Use of friend function to access private data of


a class
Addition and multiplication of two complex
number using operator overloading

3
4

Use of this pointer

Multiple and multilevel inheritance

Hybrid and hierarchical inheritance

Late binding through virtual function

4
6
9

Page | 2

11
15
20

1 .Implementation of class and member function by student


name, class and function for grade
#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;
class student
{ int roll;
int n;
public:
void reg()
{cout<<"name of student= ";
cin>>name;
cout<<
"enter the roll no=";
cin>>roll;
;
}
void display()
{
cout<<"enter the marks=";
cin>>n;
if(n>=85)
{cout<<"Grade=A+";}
else if((n>=75)&&(n<85))
{cout<<"Grade=A";}
else if((n>=65)&&(n<75))
{cout<<"Grade=B";}
else if((n>=50)&&(n<65))
{cout<<"Grade=C";}
else if((n>=40)&&(n<50))
{cout<<"Grade=D";}
else
{
cout<<"FAIL";}
}
};
int main()
{student s;
s.reg();
s.display();
getch();
}

Page | 3

2. Use of friend function to access private data of a class


#include<iostream.h>
#include<conio.h>
class mahesh;
class mukesh
{
int money;
public:
void getdat()
{
cout<<"enter the money with mukesh:";
cin>>money;
}
friend void comp(mukesh m1, mahesh m2);
};

Page | 4

class mahesh
{
int money;
public:
void getdat()
{
cout<<"enter the money with mahesh:";
cin>>money;
}
friend void comp(mukesh m1, mahesh m2);
};

void comp(mukesh m1, mahesh m2)


{
if(m1.money>m2.money)
cout<<"mukesh is richer than mahesh";
else
cout<<"mahesh is richer than mukesh";
}
void main()
{ mukesh m1;
mahesh m2;
m1.getdat();
m2.getdat();
Page | 5

comp(m1,m2);
getch();
}

3. Addition and multiplication of two complex number using


operator overloading
#include<iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
class complex
{
float real;
float img;
public:
complex(){}
complex(float a, float b)
{
real=a;
Page | 6

img=b;
}
void show()
{
if(img>0)
cout<<real<<"+j"<<img<<endl;
else
cout<<real<<"-j"<<(-1)*img<<endl;
}
complex operator+(complex);
complex operator*(complex);
};
complex complex::operator+(complex c2)
{
complex c3;
c3.real=real+c2.real;
c3.img=img+c2.img;
cout<<" addtion = ";
return(c3);}
complex complex::operator*(complex c2)
{
complex c4;
c4.real=(real*c2.real)-(img*c2.img);
c4.img=(img*c2.real)+(real*c2.img);
cout<<" multiplication= ";
Page | 7

return(c4);
}
int main()
{
complex c1(5.5,4.6);
complex c2(6.7,-9.5);
complex c3;
complex c4;
c1.show();
c2.show();
c3=c1+c2;
c3.show();
c4=c1*c2;
c4.show();
getch();}

Page | 8

4. Use of this pointer


#include<iostream.h>
#include<conio.h>
#include<string.h>
class person
{ char name[30];
int age;
public:
person(){};
person(char *s,int age);
void display();
person& greater(person &p1);
}
person::person(char *s,int a)
{
strcpy(name,s);
age=a;
}
void person::display()
{
cout<<"NAME OF OLDER PERSON IS:"<<name;
cout<<"HIS AGE IS:"<<age;
}
person& person::greater(person &p1)
Page | 9

{
if(age>p1.age)
return (*this);
else
return p1;
}
void main()
{
person p("RAHUL",30);
person p1("\nSURESH",25);
person p2;
p2=p.greater(p1);
p2.display();
getch();

Page | 10

5. Multiple and multilevel inheritance


A) Multiple inheritances:
#include<iostream.h>
#include<conio.h>
class ram
{
public:
int m1;
void getdata1(int x)
{
m1=x;
Page | 11

}
};
class balram
{
public:
int m2;
void getdata2(int y)
{
m2=y;
}
};

class total:public ram,public balram


{
int t;
public:
void sum()
{
t= m1+ m2;
}
void display()
{
cout<<"total marks: "<<t;
}
};
Page | 12

void main()
{
clrscr();
total a;
a.getdata1(45);
a.getdata2(35);
a.sum();
a.display();
getch();
}

B) Multilevel inheritances:
#include<iostream.h>
#include<conio.h>
Page | 13

class student
{
protected: int rollno;
public:
void getnumber(int a)
{
rollno=a;
}
void putnumber()
{
cout<<"ROLL NO:"<<rollno;
}};
class marks:public student
{
public:
int s1,s2;
void getm(int a,int b)
{
s1=a;
s2=b;
}};
class result:public marks
{
int total;
public:
Page | 14

void sum()
{
total=s1+s2;
}
void show()
{
putnumber();
cout<<"TOTAL MARKS"<<total<<endl;
}};
void main()
{ clrscr();
result r;
r.getnumber(13);
r.getm(75,99);
r.sum();
r.show();
getch();
}

Page | 15

6. Hybrid and hierarchical inheritance


A) Hybrid inheritance
#include<iostream.h>
#include<conio.h>
class student
{
public:
void name()
{
cout<<"\n the name of stuent is Binit:";
}
};

class marks:public student


{

Page | 16

public:
void roll()
{
cout<<"\n the roll no of the student is 1120013";
}
void mark()
{
cout<<"\n the marks obtained is 96:";
}
};

class sports
{
public:
void medal()
{
cout<<"\n Binit has won gold medal";
}
};

class result: public marks, public sports


{
public:
void rs()
{
Page | 17

cout<<"\n The result is:";


}
};

void main()
{
clrscr();
result r;
r.rs();
r.name();
r.mark();
r.medal();
getch();
}

Page | 18

B) Hierarchical inheritance
#include<iostream.h>
#include<conio.h>
class k
{
public:
int a;
void getnumber()
{
cout<<"enter the number:";
cin>>a;
}};
class b:public k
{
public:
void square()
{
getnumber();
cout<<"\n square of the number is:"<<(a*a);
}};
class c:public k
{
Page | 19

public:
void cube()
{
getnumber();
cout<<"\n the cube of the number is:"<<(a*a*a);
}};

void main()
{
clrscr();
b b1;
b1.square();
c c1;
c1.cube();
getch();
}

Page | 20

7. Late binding through virtual function


#include<iostream.h>
#include<conio.h>
class base
{
public:

virtual void show()


{
cout<<"\n This show function is of base class";
}
void display()
{
cout<<"\n Base Class";
Page | 21

}
};

class derived:public base


{
public:
void show()
{
cout<<"\n This show function is of derived class";
}
void display()
{
cout<<"\n Derived Class";
}

};
void main()
{
clrscr();
base b;
base *bptr;
bptr=&b;
bptr->show();
bptr->display();
Page | 22

derived d;
bptr=&d;
bptr->show();
bptr->display();
getch();
}

Page | 23

You might also like