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

Lab Exercise 01

qnsdjjkdd

Uploaded by

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

Lab Exercise 01

qnsdjjkdd

Uploaded by

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

Lab Exercise: OOP in C++

Objective:
To understand and implement basic Object-Oriented Programming concepts
such as classes, objects, inheritance, polymorphism, and encapsulation in C+
+.
Exercise Steps:
1. Creating a Class and Object:

Task: Define a class Person with the following attributes: name, age, and gender.
Implement a method to display the details of the person.

Explanation: This task introduces the concept of a class and how to create
objects from it. A class is a blueprint for objects, and it can contain attributes
(data members) and methods (member functions).

Class Diagram for Person Class

Code Example:

#include <iostream> // Include the iostream library for input and output
using namespace std; // Use the standard namespace

// Define the Person class


class Person {
public:
string name; // Attribute to store the name
int age; // Attribute to store the age
char gender; // Attribute to store the gender
// Method to display the details of the person
void displayDetails() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
}
};

int main() {
Person person1; // Create an object of the Person class
person1.name = "John Doe"; // Set the name attribute
person1.age = 30; // Set the age attribute
person1.gender = 'M'; // Set the gender attribute

person1.displayDetails(); // Call the method to display details

return 0; // Return 0 to indicate successful execution


}
2. Encapsulation:

Task: Modify the Person class to make the attributes private and provide
public getter and setter methods.

Explanation: Encapsulation is the concept of wrapping data and methods


that operate on the data within a single unit (class). It helps to protect the
data from outside interference and misuse.

Class Diagram for Person Class with Encapsulation


Code Example:

// Define the Person class with encapsulation


class Person {
private:
string name; // Private attribute to store the name
int age; // Private attribute to store the age
char gender; // Private attribute to store the gender

public:
// Setter method for name
void setName(string n) { name = n; }
// Setter method for age
void setAge(int a) { age = a; }
// Setter method for gender
void setGender(char g) { gender = g; }

// Getter method for name


string getName() { return name; }
// Getter method for age
int getAge() { return age; }
// Getter method for gender
char getGender() { return gender; }

// Method to display the details of the person


void displayDetails() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
}
};

int main() {
Person person1; // Create an object of the Person class
person1.setName("John Doe"); // Set the name using the setter method
person1.setAge(30); // Set the age using the setter method
person1.setGender('M'); // Set the gender using the setter method

person1.displayDetails(); // Call the method to display details

return 0; // Return 0 to indicate successful execution


}
3. Inheritance:

Task: Create a derived class Student that inherits from Person and adds an
attribute studentID. Implement a method to display the student’s details.

Explanation: Inheritance allows a class to inherit attributes and methods


from another class. The class that inherits is called the derived class, and the
class from which it inherits is called the base class.

Class Diagram for Student Class Inheriting from Person


Code Example:

// Define the Student class that inherits from Person


class Student : public Person {
private:
int studentID; // Private attribute to store the student ID

public:
// Setter method for studentID
void setStudentID(int id) { studentID = id; }
// Getter method for studentID
int getStudentID() { return studentID; }

// Method to display the details of the student


void displayDetails() {
Person::displayDetails(); // Call the base class method
cout << "Student ID: " << studentID << endl;
}
};

int main() {
Student student1; // Create an object of the Student class
student1.setName("Jane Doe"); // Set the name using the setter method
student1.setAge(20); // Set the age using the setter method
student1.setGender('F'); // Set the gender using the setter method
student1.setStudentID(12345); // Set the student ID using the setter
method

student1.displayDetails(); // Call the method to display details

return 0; // Return 0 to indicate successful execution


}

4. Polymorphism:

Task: Demonstrate polymorphism by creating a base class Shape with a


virtual method area(). Create derived classes Circle and Rectangle that
override the area() method.

Explanation: Polymorphism allows methods to do different things based on


the object it is acting upon. It is achieved using method overriding and
virtual functions.

Class Diagram for Shape, Circle, and Rectangle Classes


Code Example:

// Define the Shape class with a pure virtual function


class Shape {
public:
virtual double area() = 0; // Pure virtual function for area
};

// Define the Circle class that inherits from Shape


class Circle : public Shape {
private:
double radius; // Private attribute to store the radius

public:
// Constructor to initialize the radius
Circle(double r) : radius(r) {}

// Override the area method


double area() override {
return 3.14159 * radius * radius; // Calculate the area of the circle
}
};
// Define the Rectangle class that inherits from Shape
class Rectangle : public Shape {
private:
double length, width; // Private attributes to store the length and width

public:
// Constructor to initialize the length and width
Rectangle(double l, double w) : length(l), width(w) {}

// Override the area method


double area() override {
return length * width; // Calculate the area of the rectangle
}
};

int main() {
Shape* shape1 = new Circle(5.0); // Create a Circle object
Shape* shape2 = new Rectangle(4.0, 6.0); // Create a Rectangle object

cout << "Area of Circle: " << shape1->area() << endl; // Display the area
of the circle
cout << "Area of Rectangle: " << shape2->area() << endl; // Display the
area of the rectangle

delete shape1; // Delete the Circle object


delete shape2; // Delete the Rectangle object

return 0; // Return 0 to indicate successful execution


}

You might also like