[go: up one dir, main page]

0% found this document useful (0 votes)
7 views12 pages

Oops C++

object oriented programming

Uploaded by

ash.n.mc.57
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)
7 views12 pages

Oops C++

object oriented programming

Uploaded by

ash.n.mc.57
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/ 12

23CS101 - Problem Solving Using C++

Study Materials
Classes and Objects:

• A class is a user-defined data type, which holds its own data members and
member functions, which can be accessed and used by creating an instance of
that class.
• A C++ class is like a blueprint for an object.
• For Example: Consider the Class of Cars.
• There may be many cars with different names and brands but all of them will
share some common properties like all of them will have 4 wheels, Speed
Limit, Mileage range, etc.
• So here, the Car is the class, and wheels, speed limits, and mileage are their
properties.
• A Class is a user-defined data type that has data members and member functions.
Classes and Objects:

• Data members are the data variables and member functions are the functions
used to manipulate these variables together, these data members and member
functions define the properties and behaviour of the objects in a Class.
• In the above example of class Car, the data member will be speed
limit, mileage, etc, and member functions can be applying brakes, increasing
speed, etc.
• But we cannot use the class as it is. We first have to create an object of the class
to use its features.
• An Object is an instance of a Class.
Access Specifier:
Object:

• When a class is defined, only the specification for the object is defined; no
memory or storage is allocated.
• To use the data and access functions defined in the class, you need to create
objects.
• Syntax to Create an Object
• We can create an object of the given class in the same way we declare the
variables of any other inbuilt data type.
Access Modifiers:

• In C++ classes, we can control the access to the members of the class using
Access Specifiers.
• Also known as access modifier, they are the keywords that are specified in the
class and all the members of the class under that access specifier will have
particular access level.
• In C++, there are 3 access specifiers that are as follows:
• Public: Members declared as public can be accessed from outside the class.
• Private: Members declared as private can only be accessed within the class itself.
• Protected: Members declared as protected can be accessed within the class and
by derived classes.
• If we do not specify the access specifier, the private specifier is applied to every
member by default.
Public:

• All the class members declared under the public specifier will be available to
everyone.
• The data members and member functions declared as public can be accessed by
other classes and functions too.
• The public members of a class can be accessed from anywhere in the program
using the direct member access operator (.) with the object of that class.
Example:
Private:

• The class members declared as private can be accessed only by the member
functions inside the class.
• They are not allowed to be accessed directly by any object or function outside the
class.
• Only the member functions or the friend functions/ friend class are allowed to
access the private data members of the class.
Example:
Protected:

• The protected access modifier is similar to the private access modifier in the
sense that it can’t be accessed outside of its class unless with the help of a friend
class.
• The difference is that the class members declared as Protected can be accessed
by any subclass (derived class) of that class as well.
Example:

You might also like