Understanding Classes and Objects in C++

Markdown

View as Markdown

Object-Oriented Programming (OOP) revolves around two fundamental concepts: classes and objects. A class serves as a blueprint for creating objects, while an object is an instance of a class that contains data and functions.

Classes help organize related data and operations into a single unit, making programs easier to maintain, reuse, and expand. This modular approach is one of the primary reasons C++ is widely used for developing large-scale software applications.

Learning Objectives

  • Understand classes and objects

  • Create classes with data members and member functions

  • Instantiate objects

  • Access class members

  • Create arrays of objects

  • Use console input and output with objects

  • Understand the role of stream classes and manipulators

What is a Class?

A class is a user-defined data type that groups variables (data members) and functions (member functions) into a single unit.

General syntax:

class ClassName

{

private:

    // data members

public:

    // member functions

};

Creating Your First Class

Example:

#include <iostream>

using namespace std;

class Student

{

public:

    int id;

    string name;

};

int main()

{

    Student student1;

    student1.id = 101;

    student1.name = "John";

    cout << "ID: " << student1.id << endl;

    cout << "Name: " << student1.name << endl;

    return 0;

}

Output

ID: 101

Name: John

What is an Object?

An object is an instance of a class.

Example:

Student student1;

The object student1 now has its own copy of the class members.

Data Members

Data members store the information associated with an object.

Example:

int id;

string name;

float marks;

Each object maintains its own values for these members.

Member Functions

Member functions define the behavior of an object.

Example:

#include <iostream>

using namespace std;

class Student

{

public:

    string name;

    void display()

    {

        cout << "Student: " << name;

    }

};

int main()

{

    Student student1;

 

    student1.name = "Priya";

    student1.display();

    return 0;

}

Output

Student: Priya

Arrays of Objects

Multiple objects can be stored in an array.

Example:

#include <iostream>

using namespace std;

class Student

{

public:

    string name;

};

int main()

{

    Student students[2];

    students[0].name = "John";

    students[1].name = "Priya";

    cout << students[0].name << endl;

    cout << students[1].name << endl;

    return 0;

}

Output

John

Priya

Console Input Using cin

The cin object is used to accept user input.

Example:

#include <iostream>

using namespace std;

class Student

{

public:

    string name;

};

int main()

{

    Student student;

    cout << "Enter Name: ";

    cin >> student.name;

    cout << "Welcome " << student.name;

    return 0;

}

Console Output Using cout

The cout object displays information on the console.

Example:

cout << "Hello";

Stream Classes

The C++ Standard Library provides stream classes for input and output.

Common stream objects include:

  • cin

  • cout

  • cerr

  • clog

These simplify communication between the program and the user.

Manipulators

Manipulators modify the format of input and output.

Examples:

endl

setw()

setprecision()

fixed

Example:

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

    double marks = 89.5678;

    cout << fixed << setprecision(2) << marks;

    return 0;

}

Output

89.57

Best Practices

  • Keep data members private whenever possible.

  • Use meaningful class and object names.

  • Keep member functions focused on a single task.

  • Use arrays of objects for managing multiple records.

  • Format console output for better readability.

Common Mistakes

  • Forgetting to create an object before accessing members.

  • Omitting the object name when calling member functions.

  • Using incorrect access specifiers.

  • Forgetting to include <iomanip> when using formatting manipulators.

 

 

Was this article helpful?

Still need help?

Contact us