Introduction to C++ | Object and Class
C++ Object and Class
In C++, the concept of an Object and a Class forms the basis of Object-Oriented Programming (OOP). A class is a blueprint or template for creating objects, and an object is an instance of a class. Classes encapsulate data for the object and define methods (functions) to manipulate that data.
What is a Class?
A Class is a user-defined data type that represents the properties (data members) and behaviors (functions or methods) of objects. It defines a set of attributes and functions that apply to all objects created from it.
What is an Object?
An Object is an instance of a class. When a class is defined, no memory is allocated, but when an object of the class is created, memory is allocated for that object’s data members.
Syntax of a Class in C++
The basic syntax to define a class is as follows:
Syntax: Class Definition
class ClassName {
public: // Access modifier
// Data members
DataType variable;
// Member functions
void functionName() {
// function body
}
};
Creating an Object of a Class
Once a class is defined, objects can be created as follows:
Syntax: Creating an Object
ClassName objectName;
Example: Defining a Class and Creating an Object
Here is an example of a simple class definition and object creation:
Code Example: Class and Object
#include <iostream>
using namespace std;
// Class definition
class Car {
public:
string brand;
int year;
// Member function
void displayDetails() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
// Creating an object of class Car
Car myCar; // Object creation
// Setting values for the object
myCar.brand = "Toyota";
myCar.year = 2020;
// Calling member function
myCar.displayDetails(); // Output: Brand: Toyota, Year: 2020
return 0;
}
Output:
Explanation of the Code:
In this example:
- The class
Car
has two data members:brand
andyear
. - The class also has a member function
displayDetails()
that displays the values ofbrand
andyear
. - In the
main()
function, an objectmyCar
of classCar
is created. - We assign values to
brand
andyear
using the objectmyCar
and then call thedisplayDetails()
function to display the information.
Access Modifiers in C++ Classes
In C++, class members (data members and functions) can be declared with different access levels using access modifiers:
- public: Members are accessible from outside the class.
- private: Members are not accessible from outside the class (default access level for data members).
- protected: Members are accessible within the class and by derived class instances (used for inheritance).
Example: Using Access Modifiers
This example demonstrates the use of private
and public
access modifiers:
Code Example: Access Modifiers
#include <iostream>
using namespace std;
class Person {
private:
string name; // Private data member
public:
// Public function to access the private data member
void setName(string n) {
name = n;
}
void displayDetails() {
cout << "Name: " << name << endl;
}
};
int main() {
Person person; // Object creation
person.setName("John Doe"); // Accessing private member through public method
person.displayDetails(); // Output: Name: John Doe
// The following line would cause a compile-time error because name is private:
// person.name = "Jane Doe"; // Error: 'string' is private within this context
return 0;
}
Output:
Pro Tip:
💡 Pro Tip
When designing classes, keep in mind that encapsulation is important to prevent unauthorized access to the internal data of an object. Using access modifiers effectively can help you control how the object interacts with other parts of the program.