Encap-Constructors OOP
Encap-Constructors OOP
Content
Content
▪ Class and Object
▪ Encapsulation
▪ Data Hiding
▪ Access specifiers.
▪ Examples.
Class
“ A class in C++ is a user-defined type or data structure
declared with keyword class that has data and functions
as its members whose access is governed by the three
access specifiers private, protected or public.”
What is the
difference between
a class and an
object ?
Example
Example
Dog Class
Encapsulation
“ ? ”
Encapsulation
Cont..
Cont..
Cont..
“Encapsulation is an Object Oriented Programming
concept that binds together the data and functions that
manipulate the data, and that keeps both safe from outside
interference and misuse”
Advantages and Features of
encapsulation
Choose an appropriate data member to represent the price, and specify why it's
declared as private.
Setter (setPrice):
Getter (getPrice):
Explain the role of the getPrice function in providing read-only access to the price.
Ensuring Encapsulation:
Elaborate on how the class design maintains encapsulation, preventing external
code from directly modifying the price through the private access modifier.
Write a C++ program that
creates a class called laptop. The
data members of the class are
brand (string), model
(string), serial (int), colour
(string), price (float),
processor speed (float), RAM
(int), screen that size(float).
Create member function will set
the individual values. Since the
RAM can be upgraded therefore
create a function that allows you
to upgrade the RAM only. In the
end, create a function that will
display all the data members.
What is a Constructor?
• A constructor is a member function with
the same name as the class.
• It is automatically called when an object of
the class is created.
• Constructors are used to initialize the
data members of the class and perform
any necessary setup operations.
• They ensure that objects are properly
initialized before they can be used.
Constructors can be overloaded to provide
different ways of initializing objects.
Example
Types of Constructors
1.Default Constructor
2.Parameterized Constructor
3.Copy Constructor
Default Constructor
• Takes no parameters.
• Purpose: Initializes object
attributes to default values.
Example:
Person() { name = "John"; age = 30; }
class Book {
Parameterized Constructor public:
Book(string title, string author, int pages) {
this->title = title;
A parameterized constructor takes one this->author = author;
or more parameters and initializes the this->pages = pages;
}
object's attributes based on those
parameters. void displayInfo() {
cout << "Title: " << title << ", Author: " << author <<
", Pages: " << pages << endl;
Book(string title, std::string author, int pages) }
{ /* Initialize attributes */ }
private:
string title;
string author;
int pages;
};
class Point {
public:
Copy Constructor Point(int x, int y) {
this->x = x;
• Creates a copy of an existing object. this->y = y;
}
• Used for creating deep copies.
Point(const Point& other) {
this->x = other.x;
Example: this->y = other.y;
}
Point(const Point& other) { /* Copy
void displayCoordinates() {
attributes */ } std::cout << "X: " << x << ", Y: " << y << std::endl;
}
private:
int x;
int y;
};
class Student {
public:
void displayInfo() {
// Default constructor
std::cout << "Roll Number: " << rollNumber << ",
Student() { Name: " << name << ", Age: " << age << std::endl;
rollNumber = 0; }
name = "Unknown";
age = 0; private:
int rollNumber;
} std::string name;
int age;
// Parameterized constructor };
Student(int roll, std::string n, int a) {
rollNumber = roll; int main() {
Student student1; // Calls the default constructor
name = n; student1.displayInfo();
age = a;
} Student student2(101, "Alice", 20); // Calls the
// Copy constructor parameterized constructor
student2.displayInfo();
Student(const Student& other) {
rollNumber = other.rollNumber; Student student3 = student2; // Calls the copy
name = other.name; constructor
age = other.age; student3.displayInfo();
}
return 0;
}
Class Task
Imagine you are tasked with creating a C++
program to represent books in BookWorld's
inventory. What attributes (data) would you
consider important to include in a Book class?
Provide at least three attributes and explain why
each is essential.
Implement the following public member functions for the BankAccount class: