What is a Class in C++?
A class is a user-defined data type in C++ that acts as a blueprint for creating objects. It
encapsulates data (attributes) and functions (methods) into a single unit, enabling object-
oriented programming (OOP). The class defines the structure, while objects are the actual
instances of that structure.
Syntax of a Class
class ClassName {
// Access Specifiers: private, public, protected
private:
// Private data members (attributes)
int attribute1;
public:
// Public data members and member functions
int attribute2;
// Member function (method) to set attribute1
void setAttribute1(int value) {
attribute1 = value;
}
// Member function to get attribute1
int getAttribute1() {
return attribute1;
}
};
Components of a Class
1. Access Specifiers:
o private: Members declared under private can only be accessed by member
functions of the class. This is the default access specifier.
o public: Members declared under public can be accessed outside the class
using objects.
o protected: Members declared under protected can only be accessed by
derived classes (used in inheritance).
2. Data Members (Attributes): These are variables that hold the data or state of the
class.
3. Member Functions (Methods): These are functions defined inside the class that
operate on the data members of the class.
What is an Object in C++?
An object is an instance of a class. It is created based on the class and can hold specific
values for its attributes. Objects use the class's methods to interact with the attributes.
Creating and Using a Class and Object
Example: Basic Class and Object
#include <iostream>
using namespace std;
class Car {
private:
string brand;
int speed;
public:
// Member function to set data
void setData(string b, int s) {
brand = b;
speed = s;
}
// Member function to display data
void display() {
cout << "Car Brand: " << brand << ", Speed: " << speed << " km/h"
<< endl;
}
};
int main() {
Car car1; // Object of the class Car
car1.setData("Toyota", 180); // Setting attributes using a member
function
car1.display(); // Displaying attributes
return 0;
}
Output:
Car Brand: Toyota, Speed: 180 km/h
Memory Allocation
The memory for the class's attributes is allocated only when an object is created.
Each object gets its own copy of the data members.
Access Specifiers in Detail
#include <iostream>
using namespace std;
class Example {
private:
int privateAttribute; // Only accessible within the class
protected:
int protectedAttribute; // Accessible by derived classes
public:
int publicAttribute; // Accessible everywhere
void setAttributes(int priv, int prot, int pub) {
privateAttribute = priv;
protectedAttribute = prot;
publicAttribute = pub;
}
void display() {
cout << "Private: " << privateAttribute
<< ", Protected: " << protectedAttribute
<< ", Public: " << publicAttribute << endl;
}
};
int main() {
Example obj;
obj.publicAttribute = 100; // Public attribute can be accessed
directly
obj.setAttributes(10, 20, 30); // Setting all attributes
obj.display();
return 0;
}
Key Points
1. Encapsulation: Classes bundle data and functions together.
2. Access Control: Use private, public, and protected to control how attributes are
accessed.
3. Reusability: Classes can be reused, and new classes can be derived from them using
inheritance.
Advanced Example: Multiple Objects
cpp
CopyEdit
#include <iostream>
using namespace std;
class Rectangle {
private:
int length, width;
public:
void setDimensions(int l, int w) {
length = l;
width = w;
}
int area() {
return length * width;
}
};
int main() {
Rectangle rect1, rect2;
rect1.setDimensions(10, 5); // Object 1
rect2.setDimensions(7, 3); // Object 2
cout << "Area of rect1: " << rect1.area() << endl;
cout << "Area of rect2: " << rect2.area() << endl;
return 0;
}
Output:
mathematica
CopyEdit
Area of rect1: 50
Area of rect2: 21
Summary
1. Class: A blueprint that defines the structure (attributes) and behavior (methods).
2. Object: An instance of a class that holds specific data and can use the class's
methods.
3. Encapsulation: Data hiding and bundling into a single unit (class).
4. Access Specifiers:
o private: Accessible only within the class.
o protected: Accessible in derived classes.
o public: Accessible from anywhere.
What is a Class in C++?
A class is a user-defined data type in C++ that acts as a blueprint for creating objects. It
encapsulates data (attributes) and functions (methods) into a single unit, enabling object-
oriented programming (OOP). The class defines the structure, while objects are the actual
instances of that structure.
Syntax of a Class
class ClassName {
// Access Specifiers: private, public, protected
private:
// Private data members (attributes)
int attribute1;
public:
// Public data members and member functions
int attribute2;
// Member function (method) to set attribute1
void setAttribute1(int value) {
attribute1 = value;
}
// Member function to get attribute1
int getAttribute1() {
return attribute1;
}
};
Components of a Class
1. Access Specifiers:
o private: Members declared under private can only be accessed by member
functions of the class. This is the default access specifier.
o public: Members declared under public can be accessed outside the class using
objects.
o protected: Members declared under protected can only be accessed by derived
classes (used in inheritance).
2. Data Members (Attributes): These are variables that hold the data or state of the
class.
3. Member Functions (Methods): These are functions defined inside the class that
operate on the data members of the class.
What is an Object in C++?
An object is an instance of a class. It is created based on the class and can hold specific
values for its attributes. Objects use the class's methods to interact with the attributes.
Creating and Using a Class and Object
Example: Basic Class and Object
#include <iostream>
using namespace std;
class Car {
private:
string brand;
int speed;
public:
// Member function to set data
void setData(string b, int s) {
brand = b;
speed = s;
}
// Member function to display data
void display() {
cout << "Car Brand: " << brand << ", Speed: " << speed << " km/h"
<< endl;
}
};
int main() {
Car car1; // Object of the class Car
car1.setData("Toyota", 180); // Setting attributes using a member
function
car1.display(); // Displaying attributes
return 0;
}
Output:
yaml
CopyEdit
Car Brand: Toyota, Speed: 180 km/h
Memory Allocation
The memory for the class's attributes is allocated only when an object is created.
Each object gets its own copy of the data members.
Access Specifiers in Detail
cpp
CopyEdit
#include <iostream>
using namespace std;
class Example {
private:
int privateAttribute; // Only accessible within the class
protected:
int protectedAttribute; // Accessible by derived classes
public:
int publicAttribute; // Accessible everywhere
void setAttributes(int priv, int prot, int pub) {
privateAttribute = priv;
protectedAttribute = prot;
publicAttribute = pub;
}
void display() {
cout << "Private: " << privateAttribute
<< ", Protected: " << protectedAttribute
<< ", Public: " << publicAttribute << endl;
}
};
int main() {
Example obj;
obj.publicAttribute = 100; // Public attribute can be accessed
directly
obj.setAttributes(10, 20, 30); // Setting all attributes
obj.display();
return 0;
}
Key Points
1. Encapsulation: Classes bundle data and functions together.
2. Access Control: Use private, public, and protected to control how attributes are
accessed.
3. Reusability: Classes can be reused, and new classes can be derived from them using
inheritance.
Advanced Example: Multiple Objects
#include <iostream>
using namespace std;
class Rectangle {
private:
int length, width;
public:
void setDimensions(int l, int w) {
length = l;
width = w;
}
int area() {
return length * width;
}
};
int main() {
Rectangle rect1, rect2;
rect1.setDimensions(10, 5); // Object 1
rect2.setDimensions(7, 3); // Object 2
cout << "Area of rect1: " << rect1.area() << endl;
cout << "Area of rect2: " << rect2.area() << endl;
return 0;
}
Output:
mathematica
CopyEdit
Area of rect1: 50
Area of rect2: 21
Summary
1. Class: A blueprint that defines the structure (attributes) and behavior (methods).
2. Object: An instance of a class that holds specific data and can use the class's methods.
3. Encapsulation: Data hiding and bundling into a single unit (class).
4. Access Specifiers:
o private: Accessible only within the class.
o protected: Accessible in derived classes.
o public: Accessible from anywhere.