OOP's Experiments
OOP's Experiments
swapTheNumbers(x,y);
return 0;
}
Output:
x after swap: 3
y after swap: 7
x after swap: 7
y after swap: 3
Program - 2
WAP to implement class and objects
Code:
#include <iostream>
using namespace std;
class Student {
public:
int semester;
int rollNo;
void details (int s, int r) {
cout<< "Student semester: " << s << endl;
cout<< "Student roll number: " << r << endl;
}
};
int main () {
Student s1;
s1.rollNo = 101;
s1.semester = 3;
s1.details(s1.semester, s1.rollNo);
return 0;
}
Output:
Student semester: 3
Student roll number: 101
Program - 8
WAP to implement simple inheritance
Code:
#include <iostream>
using namespace std;
class Vehicle {
public:
int wheels;
int noOfSeats;
};
class Car: public Vehicle {
public:
void properties(int wheels, int noOfSeats){
cout<< "No. of wheels are: " << wheels << endl;
cout<< "No. of seats are: " << noOfSeats << endl;
}
};
int main() {
Car c1;
c1.wheels = 4;
c1.noOfSeats = 4;
c1.properties(c1.wheels, c1.noOfSeats);
return 0;
}
Output:
No. of wheels are: 4
No. of seats are: 4
Program - 9
WAP to implement multiple inheritance
Code:
#include <iostream>
using namespace std;
class Mammal {
public:
Mammal() {
cout << "Mammals can give direct birth" << endl;
}
};
class WingedAnimal {
public:
WingedAnimal () {
cout << "Winged animal can flap" << endl;
}
};
class Employee{
public:
Employee(int x){
cout<<"Default Constructor Invoked For Employee NO: " << x <<endl;
}
};
int main(){
Employee e1 = Employee(101);
Employee e2 = Employee(102);
return 0;
}
Output:
Default Constructor Invoked For Employee NO: 101
Default Constructor Invoked For Employee NO: 102
Program - 7
WAP to implement destructor
Code:
#include <iostream>
using namespace std;
class Employee
{
public:
Employee()
{
cout<<"This is an Employee Class"<<endl;
}
~Employee()
{
cout<<"Destructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1;
Employee e2;
return 0;
}
Output:
This is an Employee Class
This is an Employee Class
Destructor Invoked
Destructor Invoked
Program - 3
WAP to implement friend function
Code:
#include <iostream>
using namespace std;
class Box
{
private:
int length;
public:
Box(): length(0) { }
friend int printLength(Box); //friend function
};
int printLength(Box b){
b.length += 10;
return b.length;
}
int main()
{
Box b;
cout<<"Length of box: "<< printLength(b)<<endl;
return 0;
}
Output:
Length of box: 10
Program - 4
WAP to implement inline function
Code:
#include <iostream>
using namespace std;
int main()
{
cout<<"Addition of 'a' and 'b' is:"<<add(2,3);
return 0;
}
Output:
Addition of 'a' and 'b' is:5
Program - 5
WAP to implement access specifier
Code:
#include <iostream>
using namespace std;
class Employee{
private:
int salary;
public:
int ID;
int main(){
Employee e1;
e1.ID = 102;
e1.employeeID(e1. ID);
return 0;
}
Output:
Employee ID: 102