classes and objects
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 ( );
};
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;
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
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
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: