CHAPTER 15
Beginner
Classes and Objects in C++
Updated: May 17, 2026
5 min read
# CHAPTER 15
Classes and Objects
1. Introduction
In Chapter 13, we learned that astruct groups data together. A Class takes this further by grouping both *data* (attributes) and *functions* (methods) into a single unit, while also hiding data from the outside world for security.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define a Class and instantiate Objects.
-
Understand and apply Access Modifiers (
public,private,protected).
- Define Member Functions inside and outside a class.
-
Understand
staticclass members.
3. Defining a Class and Object
A class is defined using theclass keyword. Just like a struct, it must end with a semicolon.
cpp
4. Access Modifiers
Access modifiers determine *who* can access the data inside a class. This is the foundation of Encapsulation.-
1.
public: Members are accessible from *outside* the class (e.g., insidemain()).
-
2.
private: Members are ONLY accessible from *inside* the class itself. (By default, all members in a C++ class are private).
-
3.
protected: Similar to private, but can also be accessed by inherited classes (Covered in Chapter 17).
cpp
5. Member Functions: Inside vs. Outside
You can declare a function inside the class, but define it *outside* the class using the Scope Resolution Operator (::). This keeps the class blueprint clean and readable.
cpp
6. Static Members
Sometimes, you want an attribute to be shared across *all* objects of a class, rather than each object having its own copy. This is a static member.
cpp
7. Mini Project: Employee Management
cpp
8. Memory-Level Explanation
When you instantiate an object on the Stack (Car myCar;), memory is allocated for its *attributes* (like brand and year). However, memory is not duplicated for its *member functions*. Functions reside in the Text Segment of RAM, and all objects of that class share the exact same function code to save memory.
9. Common Mistakes
-
Forgetting
public:: Because classes default toprivate, if you forget thepublic:label, you won't be able to use your class inmain().
-
Calling non-static functions via Class Name:
Car::honk()is invalid unlesshonk()is a static function. You must call it on an object:myCar.honk().
10. Exercises
-
1.
Create a
Bookclass with private attributes fortitle,author, andpages. Create public methods to set and get these values.
-
2.
In the
BankAccountexample, add awithdraw()method that ensures the balance doesn't go below 0.
11. MCQ Quiz with Answers
Question 1
What is the default access modifier for members of a class in C++?
Question 2
Which operator is used to access public members of an object?
Question 3
Which access modifier allows members to be accessed from main()?
Question 4
What is the Scope Resolution Operator used for defining functions outside a class?
Question 5
A static class variable is:
Question 6
Where must a static class variable be initialized?
class Person { string name; };, can main() access name?
a) Yes b) No, because it is private by default
Answer: b) No, because it is private by default
Q8. Are member functions duplicated in memory for every object created? a) Yes b) No, objects share the same function code in memory Answer: b) No, objects share the same function code in memory
Question 9
What is a method?
Question 10
Why use private access modifiers?
12. Interview Questions
-
Q: What is the exact difference between a
structand aclassin C++?
-
Q: Explain how
staticmember variables differ from normal member variables.
-
Q: Why do we define functions outside the class using
::instead of just writing them inside the class?
13. Summary
Classes bundle data and behavior. Using access modifiers (public, private), you can implement Encapsulation, ensuring sensitive data is only modified through safe, controlled public methods. Static members allow data to be shared across all instances of a class.