Class and Objects

 Introduction to Class and Objects

In C++, a class is a user-defined data type that contains both data members (attributes/variables) and member functions (methods) that operate on that data. It acts as a blueprint for creating objects. A class specifies the properties (variables) and behaviors (functions) that the objects created from it will have.

Class Declaration Syntax

class class_name {
// Access Specifiers
};

Access Modifiers

In C++, access modifiers (also called access specifiers) are used to control the accessibility or visibility of class members such as variables and functions. They determine where the class members can be accessed from.

There are three main types of access modifiers in C++:

  1. Public
    Members declared as public can be accessed from anywhere in the program, including outside the class.

  2. Private
    Members declared as private can only be accessed within the class itself. They cannot be accessed directly from outside the class.

  3. Protected
    Members declared as protected can be accessed within the class and by derived (child) classes, but not directly from outside the class.

Example Syntax

class Example {
public:
int a; // accessible everywhere

private:
int b; // accessible only inside the class

protected:
int c; // accessible inside the class and derived classes
};  

Objects

In C++, an object is an instance of a class. It represents a real-world entity and is used to access the data members and member functions defined in the class. When a class is created, it only defines the structure. An object is created from that class to use its properties and behaviors.

Example Syntax

class Car {
public:
int speed;
};

int main() {
Car c1; // c1 is an object of class Car
}

Declaring and Initializing an Object

In C++, declaring an object means creating a variable of a class type. After declaring the object, it can be initialized by assigning values to its data members.

Object Declaration Syntax
class_name object_name;

Example
class Student {
public:
int id;
string name;
};

int main() {
Student s1; // Object declaration
s1.id = 1; // Initializing data member
s1.name = "Ram";
}

In this example, s1 is an object of the Student class. The values of id and name are assigned after the object is created.

Types of Initialization

 In C++, objects of a class can be initialized in different ways depending on how values are assigned to the data members. The common types of initialization are:

  1. Direct Initialization
    In this method, values are assigned to the object at the time of its creation using a constructor

    class Student {
    public:
    int id;
    Student(int i) {
    id = i;
    }
    };
    Student s1(10);
    
    
    1. Copy Initialization
      In copy initialization, one object is used to initialize another object.

    class Student {
    public:
    int id;
    };
    
    Student s1;
    Student s2 = s1;

    3 Default Initialization
    When an object is created without giving any value, it is called default initialization.

class Student {
public:
int id;
};
Student s1;

In default initialization, the object is created but its data members may not have specific values unless they are assigned later.

Access Specifiers in C++ (with Examples)

Access specifiers are keywords used in a class to control the visibility and accessibility of class members (variables and functions). The three main access specifiers are public, private, and protected.

class Student {
public:
int age;
};

int main() {
Student s1;
s1.age = 20; // Accessible outside the class
} 

Here, age is public, so it can be accessed directly using the object s1.

2. Private

Private members can be accessed only within the class. They cannot be accessed directly from outside the class.

Example

class Student {
private:
int age;
};

int main() {
Student s1;
// s1.age = 20; // Error: cannot access private member
}

3. Protected

Protected members can be accessed within the class and by derived (child) classes, but not directly from outside the class.

Example
 
class Parent {
protected:
int x;
};

class Child : public Parent {
public:
void setValue() {
x = 10; // Accessible in derived class
}
};

Here, x is protected, so it can be accessed in the Child class, which inherits from Parent.

Class Methods and Data Members: 

In C++, a class contains data members and member functions (methods) that define the properties and behaviors of objects.

 Data Members

Data members are the variables declared inside a class. They store the data or attributes of an object. Example

class Student {
public:
int id; // Data member
string name; // Data member
};

In this example, id and name are data members that store information about a student.

Class Methods (Member Functions)

Class methods are the functions defined inside a class that operate on the data members of the class. They define the behavior or actions of the objects.

class Student {
public:
int id;

void display() { // Class method
cout << id;
}
};

In this example, display() is a class method that prints the value of id.

Example of Class with Data Members and Member Functions

The following program shows a class that contains data members and member functions. The data members store information, and the member functions are used to set and display that information.

#include <iostream>
using namespace std;

class Person {
private:
string name; // Data member for storing the name
int age; // Data member for storing the age

public:
// Member function to set name and age
void setData(string n, int a) {
name = n;
age = a;
}

// Member function to display person details
void displayData() {
cout << "Name: " << name << "\nAge: " << age << endl;
}
};

int main() {
Person person1; // Create an object of Person class

// Set data for person1
person1.setData("Alice", 25);

// Display person1 details
person1.displayData();

return 0;
}

Write a program that demonstrates the use of public, private, and protected access specifiers in a class. Declare a class “Student” with private data members like “name” and “age”. Declare a public method “display” to display the name and age of the student. Demonstrate the use of public, private, and protected access specifiers in the
program.

#include <iostream>
using namespace std;

class Student {
private:
string name;  // Private data member
int age;      // Private data member

protected:
int roll; // Protected data member

public:
// Public method to set student data
void setData(string n, int a, int r) {
name = n;
age = a;
roll = r;
}
// Public method to display student details
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Roll No: " << roll << endl;
}
};

int main() {
Student s1; // Object of Student class

// Accessing public function
s1.setData("Ram", 16, 5);
s1.display();

return 0;
}
Output
Name: Ram
Age: 16
Roll No: 5 

Write a C++ program to create a class named Student with data members name and roll. Create an object and display the student details.

#include <iostream>
using namespace std;

class Student {
public:
string name;
int roll;

void display() {
cout << “Name: ” << name << endl;
cout << “Roll No: ” << roll << endl;
}
};

int main() {
Student s1; // object creation

s1.name = “Ram”;
s1.roll = 10;

s1.display();

return 0;
}

Write a C++ program to create a class Rectangle that calculates the area using a member function.

#include <iostream>
using namespace std;

class Rectangle {
public:
int length, breadth;

void input() {
cout << “Enter length and breadth: “;
cin >> length >> breadth;
}

int area() {
return length * breadth;
}
};

int main() {
Rectangle r;

r.input();
cout << “Area = ” << r.area();

return 0;
}

Write a C++ program using a class BankAccount with private data members. Use public member functions to deposit and display balance.

#include <iostream>
using namespace std;

class BankAccount {
private:
float balance;

public:
void deposit(float amount) {
balance = amount;
}

void display() {
cout << “Balance: ” << balance << endl;
}
};

int main() {
BankAccount acc;

acc.deposit(5000);
acc.display();

return 0;
}

Write a C++ program to create a class Book and display details of two books using objects.

#include <iostream>
using namespace std;

class Book {
public:
string title;
float price;

void show() {
cout << “Title: ” << title << endl;
cout << “Price: ” << price << endl;
}
};

int main() {
Book b1, b2;

b1.title = “C++ Programming”;
b1.price = 450;

b2.title = “Data Structures”;
b2.price = 550;

b1.show();
cout << endl;
b2.show();

return 0;
}

Write a C++ program to create a class Employee having data members name and salary. Input and display employee details using member functions.

#include <iostream>
using namespace std;

class Employee {
public:
string name;
float salary;

void input() {
cout << “Enter name and salary: “;
cin >> name >> salary;
}

void display() {
cout << “Name: ” << name << endl;
cout << “Salary: ” << salary << endl;
}
};

int main() {
Employee e;
e.input();
e.display();
return 0;
}

Create a class Car with data members brand and price. Display details of two cars using objects.

#include <iostream>
using namespace std;

class Car {
public:
string brand;
float price;

void show() {
cout << “Brand: ” << brand << endl;
cout << “Price: ” << price << endl;
}
};

int main() {
Car c1, c2;

c1.brand = “Toyota”;
c1.price = 3000000;

c2.brand = “Hyundai”;
c2.price = 2500000;

c1.show();
cout << endl;
c2.show();

return 0;
}

Compiled By Er. Basant Kumar Yadav

error: Content is protected !!
Scroll to Top