Classes & Objects
Lecturer: Dr. Rodnie K. Mafa (PhD)
Office Block 1: Office no. 17
Office line: 493 1102
After reading this lesson, you will be able to:
1. Define the concept of class and objects in object-
oriented programming.
Lesson 2. Explain how to access the members of a class.
Objectives 3. Describe the three visibility modes: public,
private, and protected.
4. Construct a class using a constructor with default
arguments.
5. Implement a destructor to manage object
lifecycle.
Definition: A class in C++ is the
building block, which leads to
Object-Oriented programming.
Class It is a user-defined data type,
which holds its own data members
and member functions, which can
be accessed and used by creating
an instance of that class.
Example..
Let’s Consider the Class of Cars. There may be many cars with different names
and brand but all of them will share some common properties like they will
have 4 wheels, Speed Limit, Mileage range and others.
Defining a Class
class Car {
public:
string brand;
int speed;
};
Object
An Object is an instance of a Class. When a class is defined, no memory is
allocated but when it is instantiated (i.e. an object is created) memory is
allocated.
Defining Class and declaring Objects
A class is defined in C++ using keyword class followed by the name of class.
The body of class is defined inside the curly brackets and terminated by a
semicolon at the end
When a class is defined, no memory or storage
is allocated. To use the data and access functions
defined in the class, you need to create objects.
Declaring
Synatx:
Objects ClassName ObjectName;
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int speed;
Objects of };
int main() {
the Class Car myCar;
Car [Link] = "Toyota";
[Link] = 120;
cout << "Brand: " << [Link] << endl;
cout << "Speed: " << [Link] << " km/h" << endl;
return 0;
}
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int speed;
Objects of };
int main() {
the Class Car myCar;
Car [Link] = "Toyota";
[Link] = 120;
cout << "Brand: " << [Link] << endl;
cout << "Speed: " << [Link] << " km/h" << endl;
return 0;
}
Relationship
between a Class &
Object
By default, the members of a class are private.
Private data members and private functions can
be accessed only by member functions of a
Cont… class.
Public members can be accessed from outside
of the class.
Member functions define the behavior of a class. They can be
defined inside or outside the class.
In Class definition;
Defining class Car {
Member public:
Functions void display() {
cout << "This is a car!" << endl;
}
};
class Car {
public:
void display(); // Function declaration
};
Outside
Class void Car::display() { // Function definition
outside class
definition cout << "This is a car!" << endl;
}
Accessing data Members and Member
functions
[Link] ();
Member function can be defined in two ways:
1. Inside the class: When a member function is
defined inside a class, it is considered to be
inline by default.
Defining 2. Outside the class. Inside Class: When a
function is large then it should be defined
Member outside the class declaration.
functions The operator ‘: :’ is known as scope resolution
operator and is used to associate member
functions to their corresponding class
Syntax
return_type Class_Name
:: function_Name;
class Sample {
private:
int num;
void setNum(int n) {
num = n;
Nesting of }
public:
Member
void display() {
Functions setNum(10); // Calling another member
function
cout << "Number: " << num << endl;
}
};
Access Specifiers
• Access specifiers control how class members (variables and functions) can be
accessed:
1. public – Accessible from outside the class.
2. private – Accessible only within the class.
3. protected – Accessible within the class and derived (child) classes.
class Test {
private:
int x; // Private member
public:
void setX(int val) { // Public member function
x = val;
Example }
int getX() {
return x;
}
};
#include <iostream> // Use standard iostream
using namespace std; // Avoid prefixing std::
class student {
private:
char name[80];
Example int rn;
float marks;
of a public: // Changed access specifier to public
void getdata();
void putdata();
program to };
// Function definitions
get and void student::getdata() {
cout << "Enter name, roll number, and marks: ";
cin >> name >> rn >> marks;
displaying }
void student::putdata() {
student }
cout << "Name: " << name << "\nRoll No: " << rn << "\nMarks: " << marks << endl;
int main() { // Changed void to int
data student st;
[Link]();
[Link]();
return 0; // Indicate successful execution
}
Constructor
• A constructor is a special member function that is automatically called when
an object is created.
• It has the same name as the class.
• It has no return type.
class Car {
public:
Car() {
cout << "Car object created!" << endl;
}
};
Example
int main() {
Car myCar; // Constructor is automatically
called
return 0;
}
Default Constructor
Default constructor is the constructor which doesn’t take any argument. It has
no parameters.
#include <iostream> // Correct #include
using namespace std;
class Construct { // Class name capitalized for clarity
public:
int a, b;
// Default Constructor
Construct() {
a = 10;
b = 20;
Example }
};
int main() {
// Default constructor is called automatically
Construct c;
cout << "a: " << c.a << endl
<< "b: " << c.b << endl; // Added endl for clarity
return 0; // Return 0 for successful execution
}
Cont..
The output:
a: 10
b: 20
A parameterized constructor accepts arguments to initialize
an object.
class Car {
public:
string brand;
Car(string b) {
brand = b;
Parameterized cout << "Brand: " << brand << endl;
Constructor }
};
int main() {
Car myCar("Toyota");
return 0;
}
Destructor
A destructor is a special member function that is automatically
invoked when an object
is destroyed.
•It has the same name as the class but starts with a tilde ~.
•It has no parameters and no return type.
class Car {
public:
~Car() {
cout << "Car object destroyed!" << endl;
}
Example };
int main() {
Car myCar;
return 0;
} // Destructor is called automatically when main() ends
Thank you