This document will introduce the fundamental concepts of Classes and Objects in C++,
including their purpose and basic structure, along with 2-3 illustrative programming examples. I
will create both DOCX and PDF versions, maintaining anonymity.
Classes & Objects in C++
Introduction to Object-Oriented Programming (OOP)
C++ is primarily an Object-Oriented Programming (OOP) language. OOP is a programming
paradigm that organizes software design around data, or objects, rather than functions and
logic. The core concepts of OOP include:
● Classes and Objects: The building blocks.
● Encapsulation: Bundling data and methods that operate on the data within a single unit
(class).
● Inheritance: A mechanism where one class acquires the properties and behaviors of
another class.
● Polymorphism: The ability of objects of different classes to respond to the same
message in different ways.
● Abstraction: Hiding complex implementation details and showing only the essential
features.
What are Classes?
A class in C++ is a user-defined data type or a blueprint/template for creating objects. It defines
the structure and behavior that its objects will have. A class specifies:
● Data Members (Attributes/Properties): Variables that hold data. These represent the
state of an object.
● Member Functions (Methods/Behaviors): Functions that operate on the data members.
These represent the actions an object can perform.
Think of a class like a blueprint for a house. The blueprint defines what a house will have
(number of rooms, kitchen, bathrooms – data members) and what actions can be performed
(open door, turn on lights – member functions). No actual house exists yet, only the design.
Syntax of a Class:
class ClassName {
public: // Access specifier
// Data Members
dataType dataMember1;
dataType dataMember2;
// Member Functions (Methods)
returnType memberFunction1(parameterList);
returnType memberFunction2(parameterList);
private: // Access specifier (covered in Document 7)
// Private data members and functions (accessible only within the
class)
protected: // Access specifier (covered in Document 7)
// Protected data members and functions (accessible within the
, class and derived classes)
}; // Don't forget the semicolon!
What are Objects?
An object is an instance of a class. When you create an object, you are essentially creating a
real-world entity based on the class blueprint. Each object has its own set of data members, but
they all share the same member functions defined by the class.
Continuing the house analogy: an object is the actual house built from the blueprint. You can
build multiple houses (objects) from the same blueprint (class), and each house will have its
own unique set of furniture, colors, etc., while still having the defined structure.
Syntax for Creating Objects:
ClassName objectName; // Creates an object of ClassName
Accessing Members
Members of an object (data members and member functions) are accessed using the dot
operator (.).
objectName.dataMember;
objectName.memberFunction(arguments);
Example Programs
Example 1: Basic Class and Object (Car)
This program demonstrates a simple Car class with data members (brand, model, year) and a
member function (displayInfo).
#include <iostream>
#include <string> // Required for std::string
// Define the Car class
class Car {
public: // Public access specifier means these members can be accessed
from outside the class
// Data Members (Attributes)
std::string brand;
std::string model;
int year;
// Member Function (Behavior)
void displayInfo() {
std::cout << "Car Info:" << std::endl;
std::cout << " Brand: " << brand << std::endl;
std::cout << " Model: " << model << std::endl;
std::cout << " Year: " << year << std::endl;
}
}; // Don't forget the semicolon after the class definition!