0% found this document useful (0 votes)
4 views19 pages

09.C++ Classes and Objects

The document provides an overview of Object-Oriented Programming (OOP) in C++, focusing on classes and objects as fundamental concepts. It explains the creation and use of classes and objects, class methods, access specifiers (public, private, protected), encapsulation, and inheritance. Additionally, it highlights the importance of attributes and methods as class members and demonstrates how to access private members using public methods.

Uploaded by

macarullo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views19 pages

09.C++ Classes and Objects

The document provides an overview of Object-Oriented Programming (OOP) in C++, focusing on classes and objects as fundamental concepts. It explains the creation and use of classes and objects, class methods, access specifiers (public, private, protected), encapsulation, and inheritance. Additionally, it highlights the importance of attributes and methods as class members and demonstrates how to access private members using public methods.

Uploaded by

macarullo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

C++

CLASSES AND OBJECTS


• What is OOP
• Classes
• Objects
• Class Methods
• Access Specifiers
• Encapsulation and
Inheritance
C++ What is OOP?


C++ Classes and Objects
Classes and objects are the two main aspects of object-oriented programming.

Look at the following illustration to see the difference between class and objects:

CLASS OBJECT CLASS OBJECT So, a class is a template for


objects, and an object is an
Fruit Apple Car Volvo
instance of a class.
Banana Audi When the individual objects
Mango Toyota are created, they inherit all
the variables and functions
from the class.
C++ Classes/Objects
C++ is an object-oriented programming language.
Everything in C++ is associated with classes and objects, along with its attributes and
methods. For example: in real life, a car is an object. The car has attributes, such as weight
and color, and methods, such as drive and brake.
Attributes and methods are basically variables and functions that belongs to the class. These
are often referred to as "class members".
A class is a user-defined data type that we can use in our program, and it works as an object
constructor, or a "blueprint" for creating objects.
C++ Class
Class − A class is a data-type that has its own members i.e. data members and member
functions. It is the blueprint for an object in object-oriented programming language. It is
the basic building block of object-oriented programming in C++. The members of a class are
accessed in a programming language by creating an instance of the class
Create a Class
To create a class, use the class keyword:
C++ Object
Object − An object is an instance of a class. It is an entity with characteristics and behavior
that are used in object-oriented programming. An object is the entity that is created to
allocate memory. A class when defined does not have a memory chunk itself which will be
allocated as soon as objects are created.
Create an Object
Create an object called "myObj"
Example and access the attributes:

In C++, an object is created from a


class. We have already created the class
named MyClass, so now we can use this
to create objects.

To create an object of MyClass, specify


the class name, followed by the object
name.

To access the class attributes (myNum and


myString), use the dot syntax (.) on the
object:
Multiple
Objects
You can create multiple
objects of one class:
C++ Class Methods
Methods are functions that belongs to the class.

There are two ways to define functions that belongs to a class:


• Inside class definition
• Outside class definition belong
In the following example, we define a function inside the class, and we name it
"myMethod".
To define a function outside the class definition, you have to declare it inside the
class and then define it outside of the class. This is done by specifiying the name of
the class, followed the scope resolution :: operator, followed by the name of the
function:
C++ Access Specifiers
In C++, there are three access specifiers:
• public - members are accessible from outside the class

• private - members cannot be accessed (or viewed) from outside the class

• protected - members cannot be accessed from outside the class, however,


they can be accessed in inherited classes. You will learn more about
Inheritance later.
In the following example, we demonstrate the differences between public and private members:
C++ Encapsulation
The meaning of Encapsulation, is to
make sure that "sensitive" data is
hidden from users. To achieve this, you
must declare class variables/attributes
as private (cannot be accessed from
outside the class). If you want others
to read or modify the value of a private
member, you can provide public get and
set methods.

Access Private Members


To access a private attribute, use
public "get" and "set" methods:
EXAMPLE EXPLAINED

The salary attribute is private, which have restricted access.

The public setSalary() method takes a parameter (s) and assigns it to the salary
attribute (salary = s).

The public getSalary() method returns the value of the private salary attribute.

Inside main(), we create an object of the Employee class. Now we can use the
setSalary() method to set the value of the private attribute to 50000. Then we call
the getSalary() method on the object to return the value.
C++ Inheritance
In C++, it is possible to inherit
attributes and methods from one
class to another. We group the
"inheritance concept" into two
categories:

•derived class (child) - the class


that inherits from another class

•base class (parent) - the class


being inherited from

To inherit from a class, use the : symbol.


C++ Access Specifiers

The third specifier, protected, is similar


to private, but it can also be accessed in
the inherited class:

You might also like