0% found this document useful (0 votes)
7 views11 pages

Define Class and Object

Uploaded by

fehminat19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

Define Class and Object

Uploaded by

fehminat19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

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;

// Member functions (behaviors)


void start() { }
void stop() { }
};

Object:

1. An object is an instance of a class.


2. It represents a real-world entity and is created using the class.
3. For example, if you have a class Car, you can create objects like car1 and car2 to
represent specific cars.

Example:

Car car1; // Object of class Car

car1.color = "Red";

Summary of Class and Object

Aspect Class Object


Definition A class is a blueprint or template that An object is an instance of a class that
defines attributes (data members) holds specific values for the attributes
and behaviors (member functions). and can execute the behaviors.
Nature It is a logical entity (conceptual It is a physical entity (memory is
structure). allocated when an object is created).
Purpose Defines the structure of objects by Represents specific instances created
specifying data and functions. from the class.
Data Storage Does not store any data. Stores data (attribute values) specific
to the object.
Reusability A single class can be used to create Each object represents a distinct
multiple objects. instance with its own state.
Access Cannot be directly accessed; only Used to access the data and methods
objects are used to interact with the defined in the class.
class's members.
Example in Defined using the class keyword. Created by declaring a variable of the
Code class type.
Members of a Class

A class is made up of members, which can be divided into two categories:

Data Members (Data):

1. These are variables or attributes of the class.


2. They hold the data or properties of the object.
3. For example, in a Student class, data members can include name, rollNumber, and
marks.

Example:

class Student {
string name; // Data member
int rollNumber; // Data member
};

Member Functions (Functions):

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:

class Student {public:


void displayDetails()
{
// Member function
cout << "Displaying student details" << endl;
}

};
Why Are Classes and Objects Important?

 Encapsulation: Groups data and methods together for better organization.


 Reusability: A class can be reused to create multiple objects.
 Scalability: You can extend a class with new attributes or methods.
 Modeling Real-World Systems: Classes and objects help simulate real-world entities and
processes in programming.

Five Important Objectives of a Class

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

Aspect Procedural Language Object-Oriented Language


Definition Focuses on a sequence of Focuses on objects, which
procedures or functions to combine data and
perform tasks. functions.
Approach Follows a top-down approach. Follows a bottom-up
approach.
Basic Unit Functions are the building blocks Classes and objects are
of a program. the building blocks of a
program.
Data Handling Data and functions are separate. Data and functions are
encapsulated within
objects.
Security Data is less secure, as it is Data is more secure, as
accessible globally. access is controlled using
encapsulation.
Reusability Reusability is achieved through Reusability is achieved
functions. through inheritance and
polymorphism.
Real-World Does not directly model real- Models real-world entities
Modeling world entities. with attributes (data) and
behaviors (functions).
Example C, Fortran, Pascal. C++, Java, Python, C#.
Languages
1. Constructor

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.

Key Characteristics of Constructors

 Same name as the class.


 No return type (not even void).
 Automatically called when an object is created.
 Can be overloaded (multiple constructors with different parameters).
 Types:
o Default Constructor: Takes no arguments.
o Parameterized Constructor: Takes arguments to initialize class members.

#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);

// Displaying the information


person1.display();
person2.display();

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).

Key Characteristics of Destructors

 Same name as the class but preceded by a ~ symbol.


 No return type and no parameters.
 Cannot be overloaded (only one destructor per class).
 Automatically called when the object is destroyed.

#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

cout << "Back in main." << endl;

return 0; // Destructor is called for obj1


}

Comparison Between Constructor and Destructor

Aspect Constructor Destructor


Cleans up resources before object
Purpose Initializes an object.
destruction.
Same as the class name, prefixed by
Name Same as the class name.
~.
Return Type No return type. No return type.
Can have parameters (parameterized
Parameters Cannot have parameters.
constructors).
Overloading Can be overloaded. Cannot be overloaded.
Automatic
Called when an object is created. Called when an object is destroyed.
Call
Inheritance in C++
Inheritance is a mechanism in C++ that allows a class (called a derived or child class) to
inherit attributes and behaviors (methods) from another class (called a base or parent
class). It enables code reuse and establishes a hierarchical relationship between classes.

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 Child : public Parent {


};

Multilevel Inheritance: A class inherits from another derived class.

class Grandparent {
public:
void greet() {
std::cout << "Hello from Grandparent!" << std::endl;
}
};

class Parent : public Grandparent {


};

class Child : public Parent {


};

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 ClassC : public ClassA, public ClassB {


};
Hierarchical Inheritance: Multiple derived classes inherit from a single base class.

class Base {
public:
void baseMethod() {
std::cout << "Base method" << std::endl;
}
};

class Derived1 : public Base {


};

class Derived2 : public Base {


};

Hybrid Inheritance: A combination of two or more types of inheritance.

Access Specifiers in Inheritance

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.

Base Class Access Public Inheritance Protected Inheritance Private Inheritance


Public Public Protected Private
Protected Protected Protected Private
Private Inaccessible Inaccessible Inaccessible
Polymorphism in C++
Polymorphism means "many forms." It allows one interface to be used for different underlying data
types or methods. There are two types of polymorphism in C++:

You might also like