0% found this document useful (0 votes)
39 views

Classes and Objects in C++

Uploaded by

bishirvv5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Classes and Objects in C++

Uploaded by

bishirvv5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Classes and Objects in C++

PREPARED BY
Dr.C.MAHIBA
ASSISTANT PROFESSOR/CSBS
2 Classes and Objects
A class in C++ serves as a foundational element for object-
oriented programming.
A Class is like a blueprint and objects are like houses built
from the blueprint
Object Oriented Programming
A class in C++ serves as a foundational element for
object-oriented programming.
User-Defined Data Type: A class is a user-defined
data type in C++. It allows programmers to
encapsulate data and behaviors into a single unit.
Blueprint for Objects: Think of a class as a
blueprint. When you create an object, you’re
essentially creating an instance of that blueprint,
inheriting its attributes and behaviors.
Contains Data and Functions: Within a class, you can
define both data members (variables) and member
functions (methods). These methods define the operations
that can be performed on the data.
Access Control: The members of a class have their access
controlled, ensuring that they can be accessed, modified,
or used based on specified permissions.
A class provides structure and organization to code,
enabling the creation of objects with defined properties
and behaviors.
Classes in C++
A class definition begins with the keyword class.
The body of the class is contained within a set of braces, {
} ; (notice the semi-colon).

class class_name Any valid


{ identifier
….
….
Class body (data member
….
+ methods)
};
Classes in C++
Within the body, the keywords private: and
public: specify the access level of the
members of the class.
the default is private.

Usually, the data members of a class are


declared in the private: section of the class
and the member functions are in public:
section.
Classes in C++

class class_name
{
private: private members or
… methods


public:
… Public members or methods


};
Classes in C++
Member access specifiers

public:
can be accessed outside the class directly.
The public stuff is the interface.

private:
Accessible only to member functions of class
Private members and methods are for internal use
only.
Class Example
 This class example shows how we can encapsulate (gather) a circle
information into one package (unit or class)

No need for others classes to


class Circle access and retrieve its value
{ directly. The class methods are
responsible for that only.
private:
double radius;
public:
void setRadius(double r); They are accessible from outside
double getDiameter(); the class, and they can access the
double getArea(); member (radius)
double getCircumference();
};
Object in C++
An object is an instance of a class. All the data members
and member functions of the class can be accessed with
the help of objects.
An object in OOP is a component which consists of
properties to make a particular data useful. For example,
let’s consider a class Student. We can access various
student details using some common property attributes
like student name, roll number, etc.
While a class is static in nature (it defines properties and
methods), an object is dynamic, representing a particular
instance of that class with its unique data.
Creating an object of a Class
Declaring a variable of a class type creates an object. You
can have many variables of the same type (class).
Instantiation
Once an object of a certain class is instantiated, a new
memory location is created for it to store its data
members and code
Syntax:
Class_name object_name;
You can instantiate many objects from a class type.
Ex) Circle c; Circle *c;
Difference between class and object
Example program

#include <iostream>
using namespace std;
class Phone {
public:
double cost;
int slots;
};
int main() {
Phone Y6;
Phone Y7;
Y6.cost = 100.0;
Y6.slots = 2;
Y7.cost = 200.0;
Y7.slots = 2;
cout << "Cost of Huawei Y6 : " << Y6.cost << endl;
cout << "Cost of Huawei Y7 : " << Y7.cost << endl;
cout << "Number of card slots for Huawei Y6 : " << Y6.slots << endl;
cout << "Number of card slots for Huawei Y7 : " << Y7.slots << endl;
return 0;
}
OUTPUT

Cost of Huawei Y6 : 100


Cost of Huawei Y7 : 200
Number of card slots for Huawei Y6 : 2
Number of card slots for Huawei Y7 : 2

You might also like