OOPS Lab Program 1 To 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Ex.no.

1 Concept of class and object creation


Program

#include <iostream>
using namespace std;

// create a class
class Room {

public:
double length;
double breadth;
double height;

double calculate_area() {
return length * breadth;
}

double calculate_volume() {
return length * breadth * height;
}
};

int main() {

// create object of Room class


Room room1;

// assign values to data members


room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;

// calculate and display the area and volume of the room


cout << "Area of Room = " << room1.calculate_area() << endl;
cout << "Volume of Room = " << room1.calculate_volume() << endl;

return 0;
}
Output

Area of Room = 1309


Volume of Room = 25132.8
Ex.no. 2a Implement Constructors within the Class
Program
// Example to show defining
// the constructor within the class

#include <iostream>
using namespace std;

// Class definition
class student {
int rno;
char name[50];
double fee;

public:
/*
Here we will define a constructor
inside the same class for which
we are creating it.
*/
student()
{
// Constructor within the class

cout << "Enter the RollNo:";


cin >> rno;
cout << "Enter the Name:";
cin >> name;
cout << "Enter the Fee:";
cin >> fee;
}

// Function to display the data


// defined via constructor
void display()
{
cout << endl << rno << "\t" << name << "\t" << fee;
}
};
int main()
{

student s;
/*
constructor gets called automatically
as soon as the object of the class is declared
*/

s.display();
return 0;
}

Output
Ex.no. 2b Implement Constructors outside the Class
Program
// defining the constructor outside the class
#include <iostream>
using namespace std;
class student {
int rno;
char name[50];
double fee;

public:
/*
To define a constructor outside the class,
we need to declare it within the class first.
Then we can define the implementation anywhere.
*/
student();

void display();
};

/*
Here we will define a constructor
outside the class for which
we are creating it.
*/
student::student()
{
// outside definition of constructor

cout << "Enter the RollNo:";


cin >> rno;
cout << "Enter the Name:";
cin >> name;
cout << "Enter the Fee:";
cin >> fee;
}

void student::display()
{
cout << endl << rno << "\t" << name << "\t" << fee;
}

// driver code
int main()
{
student s;
/*
constructor gets called automatically
as soon as the object of the class is declared
*/

s.display();
return 0;
}

Output
Ex.no. 3 Implementation of Friend function
Program
#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
Ex.no. 4a Implementation of Operator Overloading
Program

#include <iostream>
using namespace std;
class Test
{
private:
int num;
public:
Test(): num(8){}
void operator ++() {
num = num+2;
}
void Print() {
cout<<"The Count is: "<<num;
}
};
int main()
{
Test tt;
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}

Output
Ex.no. 4b Implementation of Function Overloading
Program

#include <iostream>
using namespace std;
class Cal {
public:
static int add(int a,int b){
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
};
int main(void) {
Cal C; // class object declaration.
cout<<C.add(10, 20)<<endl;
cout<<C.add(12, 20, 23);
return 0;
}

Output
Ex.no. 5 Implementation of Inheritance
Program

#include <iostream>
using namespace std;

// base class
class Animal {

public:
void eat() {
cout << "I can eat!" << endl;
}

void sleep() {
cout << "I can sleep!" << endl;
}
};

// derived class
class Dog : public Animal {

public:
void bark() {
cout << "I can bark!" << endl;
}
};

int main() {
// Create object of the Dog class
Dog dog1;

// Calling members of the base class


dog1.eat();
dog1.sleep();

// Calling member of the derived class


dog1.bark();

return 0;
}

Output
Ex.no. 6 Implementation of Polymorphism
Program

#include <iostream>
using namespace std;

class DotNetTricks {
public:
void print() {
cout << "Welcome to DotNetTricks" << endl;
}
};

class ScholarHat : public DotNetTricks {


public:
void print() {
cout << "Welcome to Thakshashila" << endl;
}
};

int main() {
ScholarHat obj1;

// Call the print() function of the ScholarHat class


obj1.print();

return 0;
}

Output
Ex.no. 7 Implementation of Exception Handling
Program

// program to divide two numbers


// throws an exception when the divisor is 0

#include <iostream>
using namespace std;

int main() {

double numerator, denominator, divide;

cout << "Enter numerator: ";


cin >> numerator;

cout << "Enter denominator: ";


cin >> denominator;

try {

// throw an exception if denominator is 0


if (denominator == 0)
throw 0;

// not executed if denominator is 0


divide = numerator / denominator;
cout << numerator << " / " << denominator << " = " << divide << endl;
}

catch (int num_exception) {


cout << "Error: Cannot divide by " << num_exception << endl;
}

return 0;
}
Output

You might also like