Oop Lab 2
Oop Lab 2
OOP
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or functions that
perform operations on the data, while object-oriented programming is
about creating objects that contain both data and functions.
Object-oriented programming has several advantages over procedural
programming:
OOP is faster and easier to execute
OOP provides a clear structure for the programs
OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and
makes the code easier to maintain, modify and debug
OOP makes it possible to create full reusable applications with
less code and shorter development time
Classes:
A collection of objects with same properties and functions is known
as class. A class is used to define characteristics of the objects.
Syntax:
class identifier
{
body of the class
}
Example
Create a class called "MyClass":
class MyClass
{ // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
Example explained
The class keyword is used to create a class called MyClass.
The public keyword is an access specifier, which specifies that members (attributes and
methods) of the class are accessible from outside the class. You will learn more
about access specifiers later.
Inside the class, there is an integer variable myNum and a string variable myString.
When variables are declared within a class, they are called attributes.
At last, end the class definition with a semicolon ;.
Access Specifiers:
The commands are used to specify the access level of class members
are known as access specifiers.
“private” access specifier
The private access specifier is used to restrict the use of class
member within the class. Any member of the class declared with private
access specifier can only be accessed within the class. It cannot be accessed
from outside the class. It is also the default access specifier.
“public” access specifier
The public access specifier is used to allow the user to access a
class member within the class as well as outside the class. Any member of
the class declared with public access specifier can be accessed from
anywhere in the program.
Object:
When a class is defined, only the specification for the object is defined; no memory or storage is
allocated.
To use the data and access functions defined in the class, we need to create objects.
int main() {
MyClass myObj; // Create an object of MyClass
Multiple Objects
You can create multiple objects of one class:
Example
// Create a Car class with some attributes
class Car {
public:
string brand;
string model;
int year;
};
int main() {
// Create an object of Car
Car carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;
1. Create a class called "Car" with a private member variable for its brand name. Add a public
member function to set the brand name, and another public member function to display the
brand name on the screen. Then, create an object of the "Car" class and use the set function to
set the brand name. Finally, use the display function to show the brand name on the screen.
2. Create a class called "Circle" with a private member variable for its radius. Add public member
functions to set the radius and to calculate the area of the circle. Then, create an object of the
"Circle" class and use the set function to set the radius. Finally, use the area function to display
the area of the circle on the screen.
3. Create a class called "Animal" with private member variables for its name and type. Add public
member functions to set the name and type, and another public member function to display the
animal's name and type on the screen. Then, create an object of the "Animal" class and use the
set functions to set the name and type. Finally, use the display function to show the animal's
name and type on the screen.
4. Create a class called "Rectangle" with private member variables for its length and width. Add
public member functions to set the length and width, and another public member function to
calculate the perimeter of the rectangle. Then, create an object of the "Rectangle" class and use
the set functions to set the length and width. Finally, use the perimeter function to display the
perimeter of the rectangle on the screen.
5. Create a class called "Fruit" with a private member variable for its name. Add a public member
function to set the name, and another public member function to display the name on the
screen. Then, create an array of objects of the "Fruit" class and use a loop to set the name of
each fruit in the array. Finally, use another loop to display the name of each fruit on the screen.