Classes-and-Objects-in-C
Classes-and-Objects-in-C
1) Class:
2) Object:
Object is a real world entity that can use for unique representation
Object is a variable
void displayInfo() {
cout << "Make: " << make << ", Model: " << model << ", Year: " << year
<< endl;
}
};
int main() {
// Create an object of the Car class
Car myCar; // Object creation
return 0;
}
@Naina
3) Encapsulation:
Real-Life Example:
Example:
#include <iostream>
using namespace std;
class Student {
private:
string name; // Private data member
public:
void setName(string n) { // Setter function
name = n;
}
int main() {
Student s;
s.setName("John Doe");
cout << "Student Name: " << s.getName() << endl;
return 0;
}
1. Header File Inclusion
#include <iostream>
using namespace std;
#include <iostream>: This includes the input-output stream library, which
allows us to use cout (for output) and cin (for input).
using namespace std;: This avoids writing std:: before cout and cin.
2. Class Definition
class Student {
private:
string name; // Private data member
class Student: Defines a class named Student.
private:: The name variable is declared private, meaning it cannot be
accessed directly outside the class.
@Naina
public:
void setName(string n) { // Setter function
name = n;
}
4. Main Function
int main() {
Student s; // Creating an object of class Student
Student s;: An object s of class Student is created.
Objects are instances of a class that store data and use methods.
return 0;
}
Returns 0, indicating successful execution.
5. Output
@Naina
Key Takeaways
1. Encapsulation:
o Data (name) is hidden from direct access.
o Access is allowed only through setName() and getName().
2. Data Security:
o Prevents unauthorized modification of name.
3. Object-Oriented Programming (OOP) Principles:
o Uses class, objects, getter, and setter.
4) Abstraction:
Data abstraction refers to providing only essential information about the data
to the outside world, ignoring unnecessary details or implementation.
Consider a real-life example of a man driving a car. The man only knows that
pressing the accelerator will increase the speed of the car or applying brakes will
@Naina
stop the car but he does not know how on pressing the accelerator the speed is
actually increasing, he does not know about the inner mechanism of the car or the
implementation of the accelerator, brakes, etc in the car. This is what abstraction is.
int main() {
Car myCar;
myCar.startEngine(); // User doesn't need to know how engine starts internally
return 0;
}
#include <iostream>
using namespace std;
class Calculator {
public:
int add(int a, int b) { // Public method (only essential details)
return a + b;
}
};
int main() {
Calculator calc;
cout << "Sum: " << calc.add(5, 3) << endl; // Output: Sum: 8
return 0;
}
Explanation:
The user only interacts with the add() function.
The internal working (how addition is performed) is hidden.
This is abstraction, as it provides a simple interface to the user.
@Naina
5) Inheritance :
What is Inheritance?
Inheritance is a feature of Object-Oriented Programming (OOP) that allows
one class to acquire properties and behaviors of another class.
It helps in code reuse and reducing duplication.
Types of Inheritance in C++
1. Single Inheritance → One parent, one child. ✅ (Example above)
2. Multiple Inheritance → Child inherits from multiple parents.
3. Multilevel Inheritance → Grandchild inherits from child (which inherits
from parent).
4. Hierarchical Inheritance → Multiple child classes inherit from one parent.
5. Hybrid Inheritance → Combination of two or more types.
6. Subclass-derived class or superclass- base class
A child inherits characteristics from their parents (like eye color, height).
Similarly, in C++, a child class (derived class) inherits from a parent class (base class).
#include <iostream>
using namespace std;
int main() {
Dog d;
d.eat(); // Inherited function from Animal class
d.bark(); // Own function of Dog class
return 0;
}
Explanation
Animal is the Base Class (Parent)
It has a function eat().
Dog is the Derived Class (Child)
It inherits the eat() function from Animal.
It has its own function bark().
Object d of Dog Class
It can call both eat() (inherited) and bark() (own method).
If we have a class A that is the Base class and another class B that is the
Derived class or child class and B is inheriting from A. Then such type of
inheritance is called Single Inheritance in C++. Suppose you have a class
Rectangle. From this class, we have written another class that is Cuboid as
shown in the below image.
Now if you have a class A then from this class there is more than one class
inheriting from A i.e. B is inheriting, C is inheriting as well as a D is inheriting. So,
when more than one class is inheriting from a Single Base Class, then such a type
of inheritance is called Hierarchical Inheritance. Suppose we have a class called
Shape. We know that Rectangle, Triangle, Circle, and so on. All these are shapes.
If there are classes A and B and from both of them class C is inheriting, then such
type of inheritance is called Multiple Inheritance in C++. There is a little different
compared to other inheritances. Multiple inheritance means our class can inherit
from more than one class in C++. So, it means for one class there can be more than
one base class. It is possible in C++ that a class can inherit from more than one
class.
@Naina
There are two subclasses i.e. B and C which are inheriting from class A. Then from
B and C there is one more class that is inheriting from B and C which is D.
Now this is a combination of hierarchical inheritance from the top and
multiple inheritances from the bottom. So, if you have this type of
inheritance then the features of base class A will be appearing in class D via
class B and class C.
@Naina
6) Polymorphism
The word “polymorphism” means having many forms.
In simple words, we can define polymorphism as the ability of a
message to be displayed in more than one form.
A real-life example of polymorphism is a person who at the same time
can have different characteristics.
Ex:- A man at the same time is a father, a husband, and an employee. So the
same person exhibits different behavior in different situations. This is called
polymorphism. Polymorphism is considered one of the important features of
Object-Oriented Programming.
Types of Polymorphism:
Access specifier
In C++, access specifiers are used to control the visibility and accessibility of
members (variables and functions) of a class. The three main access specifiers in
C++ are:
1. Public:
Members declared as public are accessible from anywhere, both inside and
outside the class.
They can be accessed directly by creating an object of the class or through a
pointer/reference to the object.
class MyClass {
public:
int x;
void display() {
std::cout << "Value of x: " << x << std::endl;
}
};
int main() {
MyClass obj;
obj.x = 10; // Accessing public member
obj.display(); // Calling public method
}
2. Private:
Members declared as private are accessible only within the class itself.
They cannot be accessed directly outside the class, not even by derived
classes.
This is used to encapsulate data and ensure that it is protected from
unauthorized access or modification.
class MyClass {
private:
int x; // Private variable
public:
void setX(int val) { x = val; } // Public method to set x
void display() {
@Naina
int main() {
MyClass obj;
obj.setX(10); // Accessing private member through public method
obj.display();
// obj.x = 20; // Error: x is private and cannot be accessed directly
}
3. Protected:
Members declared as protected are accessible within the class and its
derived classes.
They are not accessible from outside the class unless through a member
function or friend class.
This is useful when you want to allow derived classes to access base
class members, but still prevent direct access from outside the class.
class Base {
protected:
int x; // Protected member
public:
void setX(int val) { x = val; }
};
class Derived : public Base {
public:
void display() {
std::cout << "Value of x: " << x << std::endl; // Derived class can
access protected member
}
};
int main() {
Derived obj;
obj.setX(10);
obj.display();
// obj.x = 20; // Error: x is protected and cannot be accessed directly
outside of Derived
}
@Naina
These access specifiers help in controlling data encapsulation and information hiding, which are
fundamental principles in object-oriented programming.