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

OOP's Experiments

Object Oriented Programming Practical
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

OOP's Experiments

Object Oriented Programming Practical
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Program - 1

WAP to swap two numbers without using third variable


Code:
#include <iostream>
using namespace std;

void swapTheNumbers (int x, int y) {


x = x + y;
y = x - y;
x = x - y;
cout << "x after swap: " << x << endl;
cout << "y after swap: " << y << endl;
}
int main(){
int x = 3;
int y = 7;
cout << "x after swap: " << x << endl;
cout << "y after swap: " << y << endl;

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 Bat: public Mammal, public WingedAnimal {


public:
Bat (){
cout<< "Bat is a flying mammal" << endl;
}
};
int main() {
Bat b1;
return 0;
}
Output:
Mammals can give direct birth
Winged animal can flap
Bat is a flying mammal
Program - 10
WAP to implement multilevel inheritance
Code:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking..."<<endl;
}
};
class BabyDog: public Dog
{
public:
void weep() {
cout<<"Weeping...";
}
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}
Output:
Eating...
Barking...
Weeping...
Program - 6
WAP to implement constructor
Code:
#include <iostream>
using namespace std;

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;

inline int add(int a, int b){


return(a+b);
}

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;

void employeeID(int id){


cout << "Employee ID: " << id << endl;
}
};

int main(){
Employee e1;
e1.ID = 102;
e1.employeeID(e1. ID);
return 0;
}
Output:
Employee ID: 102

You might also like