OOPs File
OOPs File
- 1
AIM: Write a program that uses a class where the member functions are defined inside a
class.
Objective: The objective of this program is to demonstrate how to use a class in C++ to
calculate the area of a rectangle. It shows how to define a class with member variables and
functions, set the dimensions of the rectangle, and compute the area.
Outcome: The program successfully calculates and displays the area of a rectangle with
given length and width. In this example, the area of rectangle with a length of 5 units and a
width of 5 units is displayed as 15 units.
Program:
#include<iostream.h>
class Rectangle
{
private:
int length; //private member variables
int width;
public:
void setDimensions(int l, int w) // function for assigning value to dimensions
{
length=l;
width=w;
}
int calculateArea()
{
return(length*width); // function to calculate area
}
};
int main()
{
Rectangle rect; // Create an object of the Rectangle class
rect.setDimensions(5, 3); // Set length and width
int area = rect.calculateArea(); // Calculate the area
cout << "The area of the rectangle is: " << area << endl; // Output area
return 0;
}
Output:
Program No. – 2
AIM: Write a program that uses a class where the member functions are defined outside a
class.
Objective: The objective of this program is to demonstrate how to use a class in C++ to
calculate the area of a circle. It shows how to declare member functions inside a class and
define them outside the class using the scope resolution operator ::
Outcome: The program successfully calculates and displays the area of a circle with given
radius. In this example ,the radius is taken to be 5 units, resulting in an area of approximately
78.53 square units.
Program:
#include<iostream.h>
class Circle
{
private:
double radius; // private member declared for radius
public:
//function to set radius of circle
void setRadius(double r);
//function to calculate area of circle
double calculateArea();
};
void Circle::setRadius(double r)
{
radius=r;
}
// Definition of calculateArea() outside the class
double Circle::calculateArea()
int main() {
cout << "The area of the circle is: " << area << endl; // Output
return 0;
}
Output:
Program No. – 3
AIM: Write a program to demonstrate the use of static data members and member function.
Objective: The objective of this program is to demonstrate the use of static data members in
a C++ class. The program shows how to use a static member variable to keep track of the
total number of objects created for the class, illustrating the concept that static members are
shared across all instances of the class.
Outcome: The program successfully counts and displays the total number of objects created.
Program
#include <iostream.h>
#include<conio.h>
class Example {
private:
static int count; // Static data member declaration
public:
Example() {
count++; // Increment static member in constructor
}
~Example() {
count--; // Decrement static member in destructor
}
Example obj1;
Example::showCount(); // Access static member function
Example obj2, obj3;
Example::showCount(); // Access static member function
Example obj4;
Example::showCount(); // Access static member function
getch(); // Wait for key press
return 0;
}
Output
Current count: 1
Current count: 3
Current count: 4
Program No. – 4
Objective: The objective of this program is to demonstrate the use of `const` data members
in a C++ class. It shows how to declare constant data members that can only be initialized
during object construction and cannot be modified afterward. This concept helps ensure that
certain attributes of an object remain unchanged throughout its lifetime.
Outcome: The program successfully creates a rectangle object with const data members for
length and width. It initializes these constants during object creation and calculates area of
rectangle using immutable values.
Program:
#include <iostream.h>
#include<conio.h>
class rectangle {
private:
const int length;
const int width;
public:
rectangle(int l,int w):length(l),width(w){}
// Function to calculate and display the area of the rectangle
void displayArea() const {
int area = length*width;
cout << "Length: " << length << ", Width: " << width<<endl;
cout << "Area of the rectangle: " << area<<endl;
}
};
int main() {
// Create a Rectangle object with length 10 and width 5
rectangle rect(10, 5);
rect.displayArea(); // Display the area of the rectangle
getch(); // Wait for key press
return 0;
}
Program No. – 5
Objective: To demonstrate multilevel inheritance in C++ where a class inherits from another
class, which in turn inherits from a third class.
Outcome: The program successfully shows how a `Dog` object inherits and uses functions
from both its parent class `Mammal` and grandparent class `Animal`.
Code:
#include<iostream.h>
#include<conio.h>
class Animal //Base class
{
public:
void eat()
{cout<<”Animal eats”<<endl;
}
};
class Mammal: public Animal // derived class
{
public:
void sleep()
{cout<<”Mammal sleeps” endl;
}
};
// Further derived class
class Dog : public Mammal {
public:
void bark() {
cout << "Dog barks" << endl;
} };
int main() {
// Create a Dog object
Dog myDog;
// Call functions from each class
myDog.eat(); // From Animal class
myDog.sleep(); // From Mammal class
myDog.bark(); // From Dog class
getch();
return 0;
}
Outcome: