National Institute of Technology, Kurukshetra
Department of Electrical Engineering
APSE LAB FILE
Submitted by: Nikhil Nagar (11610341) Section: E-8
EXPERIMENT NO:- 1
Problem:- Write a program to use the friend function inorder to access
the private data of class.
Programme:-
#include<iostream>
using namespace std;
class complex
public:
int a,b;
void get_data()
int x,y;
cout<<"enter two numbers"<<endl;
cin>>x>>y;
a=x;
b=y;
void show_data()
cout<<"\na="<<a<<"\nb="<<b;
friend complex add(complex c1,complex c2)
complex temp;
temp.a=c1.a+c2.a;
temp.b=c1.b+c2.b;
return(temp);
};
int main()
complex c1,c2,c3;
c1.get_data();
c2.get_data();
c3=add(c1,c2);
c3.show_data();
return 0;
}
RESULT:-
EXPERIMENT:-2
Problem:- Write a program to implement basic structure of class and
member function by reading and printing a student name and marks and also
write a function to assign a grade to the student.
Programme:-
#include<iostream>
using namespace std;
class student
public:
string name;
int roll_no;
int marks;
void get_data();
void print_data();
void print_grade();
};
void student::get_data()
cout<<"enter name ,roll number, marks";
cin>>name>>roll_no>>marks;
}
void student::print_data()
cout<<"display";
void student::print_grade()
if(marks>=85)
cout<<"A+";
else if(marks<85&&marks>=75)
cout<<"A";
else if(marks<75&&marks>=65)
cout<<"B";
else if(marks<65&&marks>=50)
cout<<"C";
else if(marks<50&&marks>=40)
cout<<"D";
else
cout<<"fail";
int main()
student s[10];
s[1].get_data();
s[1].print_data();
s[1].print_grade();
return 0;
}
RESULT:-
Problem(b):-Write a program to overload + and * operators to add
and multiply two numbers.
Programe:-
#include<iostream>
class complex
int a;
int b;
public:
void get(int x,int y)
a=x;
b=y;
void show()
if (b>=0)
std::cout<<a<<"+j"<<b<<"\n";
if(b<0)
std::cout<<a<<"-j"<<-b<<"\n";
}
complex operator+(complex c);
complex operator*(complex c);
};
complex complex :: operator+(complex c)
complex temp;
temp.a = a +c.a;
temp.b = b +c.b;
return temp;
}
complex complex :: operator*(complex c)
complex temp;
temp.a =a*(c.a)-b*(c.b);
temp.b= a*(c.b)+b*(c.a);
return temp;
int main()
complex a,b,c,d;
a.get(4,-4);
b.get(2,3);
c= a+b;
d= a*b;
a.show();
b.show();
std:: cout<<"sum of two vectors ="<<"\n";
c.show();
std:: cout<<"product of two vectors ="<<"\n";
d.show();
return 0;
}
RESULT:-
EXPERIMENT NO:-3
Problem:- Write a program to implement multiple and multi level
inheritance.
Programme:-
#include<iostream>
using namespace std;
class animal
public:
void show_data()
cout<<"all are animals"<<endl;
};
class dog:public animal
public:
void show_data1()
cout<<"all dogs are animal"<<endl;
}
};
class cat:public animal
public:
void show_data2()
cout<<"all cats are animal"<<endl;
};
class puppy:public dog
public:
void show_data3()
cout<<"all puppies are dog"<<endl;
};
int main()
animal a1;
dog d1;
cat c1;
puppy p1;
a1.show_data();
d1.show_data1();
c1.show_data2();
p1.show_data3();
return 0;
RESULT:-
EXPERIMENT NO:-4
Problem:- Write a program to show the use of pointer.
Programme:-
#include<bits/stdc++.h>
using namespace std;
class Student
public:
int Roll_no;
string name;
long long int phone_no;
void get_input()
cout<<"enter the roll no:-";
cin>>Roll_no;
cout<<"enter the name:-";
cin>>name;
cout<<"enter the phone no:-";
cin>>phone_no;
void info()
cout<<"Roll no:-"<<Roll_no<<endl;
cout<<"name:-"<<name<<endl;
cout<<"phone no:-"<<phone_no<<endl;
};
int main()
Student s1 ,*p_st;
s1.get_input();
cout<<"the information of the student is:--"<<endl;
p_st=&s1;
p_st->info();
return 0;
}
RESULT:-
EXPERIMENT:-5
Problem:- Write a program to implement Hybrid and Hierarchial
Inheritance.
Programme:-
#include<iostream>
using namespace std;
class animal
public:
void show_data()
cout<<"all are animals"<<endl;
};
class dog:public animal
public:
void show_data1()
cout<<"all dogs are animal"<<endl;
show_data();
}
};
class cat:public animal
public:
void show_data2()
cout<<"all cats are animal"<<endl;
};
class puppy:public dog
public:
void show_data3()
cout<<"all puppy are dogs"<<endl;
};
int main()
puppy p;
cat c;
p.show_data3();
p.show_data1();
c.show_data2();
return 0;
}
RESULT:-
EXPERIMENT:- 6
Problem:- Write a program to Achieve Late Binding through Virtual
Functions.
Programme:-
#include<iostream>
using namespace std;
class Base
public:
virtual void show() { cout<<" In Base \n"; }
};
class Derived: public Base
public:
void show() { cout<<"In Derived \n"; }
};
int main(void)
{
Base *bp = new Derived;
bp->show(); // RUN-TIME POLYMORPHISM
return 0;
}
Result:-
EXPERIMENT:-7
Problem:- Apply the concept of the inheritance to write a program to
estimate the bills of a various consumers to two part tariff.
Programme:-
#include<iostream>
using namespace std;
class bill
public:
int units,ammount,base=200;
void get_data()
cout<<"enter the uses units"<<endl;
cin>>units;
void show_data()
cout<<"units: "<<units<<endl;
void total_bill()
{
if(units<=base)
ammount=units*6;
cout<<"Your bill: "<<ammount;
else if(units>=base)
ammount=(base*6)+(units-200)*10;
cout<<"Your bill: "<<ammount;
else
cout<<"error"<<endl;
};
};
int main()
bill b;
b.get_data();
b.show_data();
b.total_bill();
return 0;
}
RESULT:-
EXPERIMENT:-8
Programme:-Write a programme to simulate game of chances using
object oriented programming paradibam.
Programme:-
#include<iostream>
using namespace std;
class game_of_chance
public:
int a,b,no_of_chance=200;
void get_data()
cout<<"who gets more heads,wins"<<endl;
cout<<"enter the totat attemts:-"<<endl;
cin>>no_of_chance;
cout<<"enter the number of heads of nikhil"<<endl;
cin>>a;
cout<<"enter the number of heads of vani"<<endl;
cin>>b;
}
void game_result()
if((a+b)!=no_of_chance)
cout<<"invalid"<<endl;
else if(a>b)
cout<<"nikhil wins"<<endl;
else
cout<<"vani wins"<<endl;
};
int main()
game_of_chance g;
g.get_data();
g.game_result();
return 0;
}
RESULT:-
EXPERIMENT:-9
Problem:- Write a program to implement function templates to find out
the roots of second order quardracti equment.
Programme:-
#include<iostream>
#include<cmath>
using namespace std;
template<class T>
T root(T a,T b,T c)
T D=b*b-4*a*c;
if(D==0)
cout<<" x ="<<-b/2*a<<endl;
else if(D>0)
float r=sqrt(D);
cout<<" x1 = "<<(-b+r)/2*a<<" x2 = "<<(-b-r)/2*a<<endl;
else
float r=sqrt(-D);
cout<<" x1 = "<<-b/2*a<<" +i"<<r/2*a<<" x2 = "<<-b/2*a<<" -
i"<<r/2*a<<endl;
};
int main()
root(2,3,4);
return 0;
}
RESULT:-