0% found this document useful (0 votes)
3 views11 pages

classes and objects

Chapter 7 discusses classes and objects in programming, defining a class as a user-defined datatype with data members and member functions. It explains class definitions, access specifiers (private, public, protected), and the syntax for declaring objects and accessing class members. Additionally, it covers member function definitions, arrays as class members, function overloading, inline functions, and friend functions, highlighting their characteristics and advantages.

Uploaded by

tan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
3 views11 pages

classes and objects

Chapter 7 discusses classes and objects in programming, defining a class as a user-defined datatype with data members and member functions. It explains class definitions, access specifiers (private, public, protected), and the syntax for declaring objects and accessing class members. Additionally, it covers member function definitions, arrays as class members, function overloading, inline functions, and friend functions, highlighting their characteristics and advantages.

Uploaded by

tan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

CHAPTER 7: CLASSES AND OBJECTS

CLASS
A class is a user defined datatype that contains data members and member functions.

CLASS DEFINITION
A class definition is a process of naming a class and its data variables and member functions.
Syntax :
class classname
{
private:
Member data;
Member functions;
public:
Member data;
Member functions;
protected:
Member data;
Member functions;
};
Example:
class student
{
private :
int regno;
char name [30];
public:
void getdata ( );
void putdata ( );
};

NOTE: Difference between Class and Class Definition


‘Class’ is a user defined datatype that contains data members and member functions whereas
‘Class Definition’ is a process of naming a class and its data variables and member
functions.

DATA MEMBERS
Properties of the class.
Example: regno, name

MEMBER FUNCTION
The set of operations performed on data members.
Example getdata()
ACCESS SPECIFIERS
The access specifiers define the scope of data. They provide security to the data.
Types of Access Specifiers
1.Private
Data member or a member function declared under private can be accessed only within the
class.
NOTE: The Default access specifier is Private
2.Public
Data member or a member function declared under public can be accessed by any function
outside the class also. (From anywhere within a program)
3.Protected
Data member or a member function declared under protected can be accessed only by the
derived class and friend function.

OBJECT
It is an instance of the class.
The syntax for declaring object is
classname objectname;
Ex. Student s;

ACCESSING CLASS MEMBERS


The members of the class are accessed using the member access operator, dot operator(.)
1.The syntax for accessing a data member of a class is
objectname . datamember;
Ex. s.name
2.The syntax for accessing a member function of a class is
objectname . Memberfunctionname(arguments);
Ex. s.getdata( );

DEFINING MEMBER FUNCTIONS


Member functions can be defined in two ways.
1.Inside the class definition
2.Outside the class definition.
1.Inside the class definition
Member function’s declaration and definition occurring at the same place inside the class is
‘Member function definition inside the class’.
Syntax
returntype functionname (argument)
{
Body of member function
}
Example:
The following program will allow the user to enter student’s register number and name
and display the same.
class student
{
private:
int regno;
char name[20];
public:
void getdata()
{
cout<<"Enter the Register Number";
cin>>regno;
cout<<"Enter the Name";
cin>>name;
}
void putdata()
{
cout<<"Register Number="<<regno;
cout<<"Name="<<name;
}
};
void main()
{
clrscr();
student s;
s.getdata();
s.putdata();
getch();
}
Output:

2.Outside the class definition


Member function’s declaration occurring inside the class and definition occurring outside the
class using scope resolution operator is ‘Member function definition outside the class’.
NOTE: The membership identity label ’classname ::’ tells the compiler that the function
belongs to the particular class.
Syntax
returntype classname :: functionname (argument declaration)
{
Body of member function
}
Example:
The following program will allow the user to enter student’s register number and name
and display the same.
class student
{
private:
int regno;
char name[20];
public:
void getdata();
void putdata();
};
void student::getdata()
{
cout<<"Enter the Register Number"<<endl;
cin>>regno;
cout<<"Enter the Name"<<endl;
cin>>name;
}
void student::putdata()
{
cout<<"Register Number="<<regno<<endl;
cout<<"Name="<<name<<endl;
}
void main()
{
clrscr();
student s;
s.getdata();
s.putdata();
getch();
}
Output:

CHARACTERISTICS OF MEMBER FUNCTION DEFINED OUTSIDE


THE CLASS
1. Number of arguments and type of arguments during declaration and definition must
be same.
2. The scope resolution operator is used while defining a function outside a class.
3. Several classes can use the same function name. A membership identity
label(classname ::) is used to identify a function of a particular class.
4. Member functions defined outside the class can easily access private data members of
the class.
5. Return type of member function indicates the type of value it returns in the end.

ARRAYS AS MEMBERS OF CLASSES


Just like how we use normal variables as data members, Arrays can also be used as data
members of the class.
Syntax : Datatype variablename[size of array];
Example: Program to read and display 5 elements using array as data members
#include<stdio.h>
#include<conio.h>
#include<iomanip.h>
class array
{
private:
int a[5],i;
public:
void getdata();
void putdata();
};
void array::getdata()
{
cout<<"Enter 5 array elements";
for(i=0;i<5;i++)
cin>>a[i];
}
void array::putdata()
{
cout<<"Array elements are as follows";
for(i=0;i<5;i++)
cout<<a[i]<<setw(3);
}
void main()
{
clrscr();
array ar;
ar.getdata();
ar.putdata();
getch();
}
Output:

ARRAYS OF OBJECTS
An array having class type elements is known as array of objects.
Example: Program to enter and display 3 students information using array of objects.
#include<iostream.h>
#include<conio.h>
class student
{
private:
int regno;
char name[10];
public:
void getdata();
void putdata();
};
void student::getdata()
{
cout<<"Enter register number";
cin>>regno;
cout<<"Enter name";
cin>>name;
}
void student::putdata()
{
cout<<"Register number="<<regno;
cout<<"Name="<<name;
}
void main()
{
clrscr();
student s[3];
int i;
for(i=0;i<3;i++)
s[i].getdata();
for(i=0;i<3;i++)
s[i].putdata();
getch();
}
Output:
DIFFERENCES BETWEEN OBJECT ORIENTED
PROGRAMMING(OOP) AND PROCEDURE ORIENTED
PROGRAMMING(POP) LANGUAGES

DIFFERENCES BETWEEN STRUCTURE AND CLASS


CHAPTER 8 : FUNCTION OVERLOADING
FUNCTION OVERLOADING
Two or more functions having the same name but differ in the number of arguments or data
types of arguments is Function Overloading.
Example:
C++ Program to compute the volume of cube, cuboid, cylinder.
#include<iostream.h>
class example
{
public : float volume(int a)
{
return(a*a*a); // volume of cube
}
float volume(float l, float b, float h)
{
return(l*b*h); //volume of cuboid
}
float volume (float r , float h)
{
return (3.14*r*r*h) ; //volume of cylinder
}
};
void main()
{
example e;
cout<<“Volume of the cube is “<<e.volume(5);
cout<<“Volume of the cuboid is “<<e.volume(5.0,6.4,7.1);
cout<<“Volume of cylinder is”<<e.volume(2.5,3.6);
getch();
}

FUNCTION SIGNATURE
The argument list of a function is called function signature.

ADVANTAGES
1. Code is executed faster.
2. Eliminates the use of different function names for the same operation.
3. It is easier to understand the flow of information and debug.
4. Code maintenance is easy.
5. Easier interface between programs and real-world objects.

INLINE FUNCTIONS
The Inline function is a function whose body is inserted at the place of its call.
Syntax
inline returntype functionname(argument declaration)
{
statements;
return(expression);
}
Example:
C++ program to find the cube of a number using inline function. (Without classes and
objects)
#include<iostream.h>
inline int cube (int a)
{
return (a*a*a);
}
void main()
{
int x;
cout<<“Enter a number “;
cin>>x;
cout<<“Cube=” <<cube(x);
}
Output:
Enter a number
2
Cube=8

ADVANTAGES OF INLINE FUNCTION


1. The inline member function are compact function calls.
2. The size of the object code is considerably reduced.
3. The speed of execution increases.
4. The readability of the program increases.
5. It avoids function calling overhead.

NOTE: The inline function may not work for one of the following reasons.
1. The inline function definition is too long or too complicated.
2. The inline function is recursive.
3. The inline function has looping constructs.
4. The inline function has a switch or goto statement.

FRIEND FUNCTION
A friend function is a non-member function that is a friend of a class which can access
protected and private data members of class.
Syntax
friend returntype friendfunctionname (classname objectname)
{
Statements;
}
Example: To add two numbers and display the sum using friend function.
#include<iostream.h>
#include<conio.h>
class addition
{
public:
int a,b;
public:
void getdata();
friend int calculate(addition obj);
};
void addition::getdata()
{
cout<<"Enter a and b";
cin>>a>>b;
}
int calculate(addition obj)
{
return (obj.a+obj.b);
}
void main()
{
addition object;
object.getdata();
cout<<"Sum="<<calculate(object);
}
Output:

CHARACTERISTICS OF FRIEND FUNCTION


1. A friend function has full access right to the private and protected members of the
class.
2. It can be invoked like any normal function.
3. The friend declaration can be placed anywhere in the class
4. The function is declared with keyword ‘friend’ but defining it doesn’t use keyword
‘friend’ or ‘::’ operator.
5. A class may have any number of friend functions and a function may be friend to any
number of classes.

You might also like