100% found this document useful (1 vote)
51 views

Classes and Objects

The document discusses classes and objects in object-oriented programming. It defines a class as a way to bind data and functions together, allowing data to be hidden from external use. A class specifies attributes like data members and member functions. An object is a variable of a class type that stores data and allows access to member functions. The document provides an example of a student class with attributes like roll number, name, and address, and a member function to print details. It describes accessing class members through objects and defining member functions inside and outside of class definitions.

Uploaded by

vdjohn
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
51 views

Classes and Objects

The document discusses classes and objects in object-oriented programming. It defines a class as a way to bind data and functions together, allowing data to be hidden from external use. A class specifies attributes like data members and member functions. An object is a variable of a class type that stores data and allows access to member functions. The document provides an example of a student class with attributes like roll number, name, and address, and a member function to print details. It describes accessing class members through objects and defining member functions inside and outside of class definitions.

Uploaded by

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

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY,

RAMAPURAM CAMPUS

OBJECT ORIENTED
PROGRAMMING

Classes and Objects

Dr.S.Veena, Associate Professor/CSE 1


Classes and Objects

Class

• A class is one of the most fundamental needs for object oriented programming. For a problem to
be solved in C++ the entities of the problem are to be represented as classes. Suppose we have to
write a program for printing student mark sheets. The student, examiner, supervisor, mark sheet,
merit list all are the entities in this case.

• Entities are represented as classes in a C++ program. The information that we are interested in
storing about these entities is known as the attributes of the class. Attributes are of two types: data
and methods (functions).

• One of the entities of the mark sheet program is the examiner. The data attributes of this entity
(examiner) may be examiner’s subject, name, address, phone number, and affiliation. Function
attributes of the examiner entity can be reading and printing details about examiners, assigning
bunch of answer sheets etc.

Dr.S.Veena, Associate Professor/CSE 2


Classes and Objects

Definition of a class

• A class is a way to bind the data and its associated functions together. It
allows the data to be hidden if necessary from external use. While defining a
class, we are creating a new abstract data type that can be treated as a built-in
data type. A class specification has two parts:

1. Class declaration – describes the type & scope of its members

2. Class function definitions – describe how the class functions are implemented.

Dr.S.Veena, Associate Professor/CSE 3


Classes and Objects

Dr.S.Veena, Associate Professor/CSE 4


Classes and Objects

 The keyword class specifies that what follows is an abstract data of type class_name.

 The body of the class is enclosed within braces and terminated by a semicolon.

 The class body contains the declaration of variables and functions. These functions and
variables are collectively called class members.

 The keywords private and public are known as visibility labels and it specify which
members are private which of them are public. These should followed by a colon.

• Private members can be accessed only within the class whereas public members can
be accessed form outside the class. By default, the members of a class are private. If
both the labels are missing, the members are private to the class.

Dr.S.Veena, Associate Professor/CSE 5


Classes and Objects

Objects
•  Defining a class will not create any object as specifying just int will not create
any variable. Once a class has been declared we can create variable of that type
by using the class name.

• For example :

item x; - creates a variable of x of type item. In C++, the class


variables are known as objects. More than one object can be created using the
statement as follows:

item x,y,z;

Dr.S.Veena, Associate Professor/CSE 6


Classes and Objects

Access Specifiers
Access specifiers are used to access the data members and member functions of the class. There are
three types of access specifiers :
 private
 public
 protected
private: the attributes that provide services (access) only to member functions are kept to be private.
private is the default access mode for class members.
public : the attributes that provide services (access) to outsiders are to be kept as public.
protected : a member declared as protected is accessible by the member functions within its class and
class immediately derived from it. It cannot be accessed by functions outside these two classes (base
and derived).

Dr.S.Veena, Associate Professor/CSE 7


Classes and Objects

class alpha
{
private: // optional
……. // visible to member functions
……. // within its class

protected:
……. // visible to member functions
……. // of its own and derived class

public:
……. // visible to all functions
……. // in the program
}

Dr.S.Veena, Associate Professor/CSE 8


Classes and Objects

• Inheritance and Accessibility :

Access Accessible from Accessible from Accessible from


Specifier Own Class Derived Class Objects Outside Class
public Yes Yes Yes

Protected Yes Yes No

Private Yes No No

Dr.S.Veena, Associate Professor/CSE 9


Classes and Objects
Functions and Data Members

•The variables declared inside the class are known as data members and the
functions as member functions. Only the member functions can have access to
the private data and private functions. But the public members can be accessed
from outside the class.

• Example:
class item
 {
int num; // variables declarations private by default
float cost;
public:
void getdata(int, float); // function declaration using prototype
void putdata();
};

Dr.S.Veena, Associate Professor/CSE 10


Classes and Objects

Functions and Data Members


Explanation:
 item  class name, which is used to declare instances of that class type.
It contains two data members and two function members. The data members
are private by default and function members are public by declaration.
getdata()  Assign values to the variables num and cost
putdata()  Display the values of the variables
Only the above functions can access the data members. Usually the data
members are declared private, the function members are declared public.

Dr.S.Veena, Associate Professor/CSE 11


Classes and Objects

Defining Member Functions:

Member functions can be defined in two ways:

1. Outside the class definition

2. Inside the class definition

Dr.S.Veena, Associate Professor/CSE 12


Classes and Objects

Dr.S.Veena, Associate Professor/CSE 13


Classes and Objects

Example:
void item :: getdata(int a,float b)
{
num = a;
cost = b;
}
 
void item ::putdata()
{
cout << “Num = “<< num <<”\nCost = “<<cost;
}
Characteristics:

 Several different classes can use the same function name. The ‘membership’ label will resolve their scope.

 Member function can access the private data while a non member function cannot.

 A member function can call another member function directly, without using dot operator.

Dr.S.Veena, Associate Professor/CSE 14


Classes and Objects

Inside the class definition:

• This method replaces the function declaration by the function definition inside the class. When the
function is defined inside the class it is treated as a inline function. Normally only small functions are defined
inside the class definition.
class item
{
int num; // variables declarations private by default
float cost;
public:
void getdata(int, float); // function declaration using prototype
void putdata()
{
cout << “Num = “<< num <<”\nCost = “<<cost;
}
};
 

Dr.S.Veena, Associate Professor/CSE 15


Classes and Objects

Accessing Class Members:

  The private data of a class can be accessed only by its member functions. Hence
main() cannot access the private data of a class. The syntax for calling the member
function is
object-name.function-name(actual-arguments);
With respect to the above example,
x.getdata(27,65.5);
It assigns the value 27 to num and 65.5 to cost. Similarly,
x.putdata();
will display the values of num and cost.
Remember a member function can be invoked only by using an object.

Dr.S.Veena, Associate Professor/CSE 16


Classes and Objects

Dr.S.Veena, Associate Professor/CSE 17


Classes and Objects

• Here, student is an example of class, which represents a real world student


(an actual student).
• The attributes of the student class are Rollno, Name, Address and
PrintDeatils.
• PrintDeatils( ) is a function attribute and others are data attributes. Student1
is an object of type student.
Note: The data types of Name and Address; string is available as data type in
C++.
 

Dr.S.Veena, Associate Professor/CSE 18


Classes and Objects

Dr.S.Veena, Associate Professor/CSE 19


Classes and Objects

Dr.S.Veena, Associate Professor/CSE 20

You might also like