Define Class and Object
Define Class and Object
Class:
1. A class is a blueprint or template that defines the structure and behavior of objects.
2. It groups related data (variables) and functions (methods) together.
3. Think of a class as a recipe for creating objects. For example, if you have a Car
class, it can define the properties (like color, model) and behaviors (like start, stop) of
a car.
Example:
class Car {
// Data members (properties)
string color;
string model;
Object:
Example:
car1.color = "Red";
Example:
class Student {
string name; // Data member
int rollNumber; // Data member
};
1. These are methods or functions that define the behavior of the object.
2. They operate on the data members and perform specific tasks.
3. For example, in a Student class, member functions can include addMarks() and
displayDetails().
Example:
};
Why Are Classes and Objects Important?
1. Encapsulation: Combine data and functions into a single unit to manage and protect
information effectively.
2. Code Reusability: Create reusable blueprints to reduce redundancy and save
development time.
3. Abstraction: Hide implementation details and expose only essential functionalities to the
user.
4. Inheritance: Allow sharing of common properties and behaviors between classes,
promoting code reuse.
5. Data Security: Protect sensitive data by controlling access using access specifiers like
private, protected, and public.
Difference between Procedural and Object-Oriented Language
A constructor is a special member function in a class that is automatically called when an object
of the class is created. It is used to initialize the data members of the class.
#include <iostream>
using namespace std;
class Person {
public:
string name;
int age;
// Constructor
Person(string n, int a) {
name = n;
age = a;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
// Creating objects using the constructor
Person person1("Alice", 25);
Person person2("Bob", 30);
return 0;
}
2. Destructor
A destructor is a special member function that is automatically called when an object goes out of
scope or is explicitly deleted. It is used to clean up resources (e.g., closing files, releasing
memory).
#include <iostream>
using namespace std;
class Demo {
public:
// Constructor
Demo() {
cout << "Constructor called." << endl;
}
// Destructor
~Demo() {
cout << "Destructor called." << endl;
}
};
int main() {
Demo obj1; // Constructor is called here
{
Demo obj2; // Constructor is called for obj2
} // Destructor is called for obj2 when it goes out of scope
Types of Inheritance
Single Inheritance: A child class inherits from a single parent class.
class Parent {
public:
void display() {
std::cout << "This is Parent class." << std::endl;
}
};
class Grandparent {
public:
void greet() {
std::cout << "Hello from Grandparent!" << std::endl;
}
};
Multiple Inheritance: A class inherits from more than one base class.
class ClassA {
public:
void methodA() {
std::cout << "Method from ClassA" << std::endl;
}
};
class ClassB {
public:
void methodB() {
std::cout << "Method from ClassB" << std::endl;
}
};
class Base {
public:
void baseMethod() {
std::cout << "Base method" << std::endl;
}
};
Public Inheritance: Public members of the base class remain public in the derived class.
Protected Inheritance: Public and protected members of the base class become protected in the
derived class.
Private Inheritance: Public and protected members of the base class become private in the
derived class.