Sie sind auf Seite 1von 7

UNIT 5

Classes and Objects

Defining the Class in C++


A class is defined in C++ programming using keyword class followed by an identifier (name
of class). Body of class contains various methods and data members enclosed within curly
braces and terminated by semi colon (;)
Syntax:
class class_name
{
// data members
….
// methods};
Accessibility modes:
There are three different access specifiers used for class:
1. private= Data members and methods declared in this scope are accessible to that block
only where it is declared. These cannot be accessed outside the block. By default, all class
members are of private scope.
2. public= Data members and methods declared in this scope are accessible throughout the
program which means these can be accessed outside block also.
3. protected= Data members and methods declared in this scope are accessible in the current
class and its derived classes. This mode is used in inheritance concept that will be
discussed later.
Instantiation of class
A class does not occupy any memory space until its object is created. An object is an instance
of a class and the process of creating an object is called instantiation of the class.
Syntax:
class_name variable_name;
Accessing Class members:
If we want to access members, then we can do this by creating objects of class.
Object_name.member_name;

Defining methods in class :


We can define methods in two different areas of a class:
1. Inside class
2. Outside class
1. Inside class
In this approach, we define the method body within class area. See the example
given below:
#include<iostream.h>
#include<conio.h>
class test
{
public:
void disp()
{
cout<<"hello";
}
};
void main()
{
clrscr();
test x;
x.disp();
getch();}
Output:
hello
2. Outside class
In this approach,we only declare the methods within class block. Scope resolution operator
(::) is used to define the methods outside class.
Syntax:
return_type class_name :: method_name( < arguments_list >);

Data Encapsulation
Data members: The data is the information about the program which affected by the
program functions. It is more sensible element of a class because all calculations are
performed on data.
Member methods: This is the part of a program that performs actions on data members of a
class.
Encapsulation or Data Hiding is an Object Oriented Programming concept that binds data and
functions together and also protects the data from outside interference and misuse.
To make a program more secure & understandable, all data members of a class are kept in
“private” or “protected” scope & member functions or methods remained in “public” scope.

Array of objects

In this concept ,we will make class’s object in array type and size of the array will be decided
by the user.
See the example below:
#include<iostream.h>
#include<conio.h>
class test
{
char name[10];
int marks;
public:
void getdata();
void putdata();
};
void test::getdata()
{
cout<<"enter name";
cin>>name;
cout<<"enter marks";
cin>>marks;
}
void test::putdata()
{
cout<<"Name="<<name<<endl;
cout<<"Marks="<<marks<<endl;
}
void main()
{
int n,i;
clrscr();
test x[3];
for(i=0;i<3;i++)
{
x[i].getdata();
x[i].putdata();
}
getch();
}
This program will input name and marks of 3 different students using getdata() and then print
the data using putdata().

Friend function

Data encapsulation feature of OOP secures data in which data members declared in “private”
scope are not accessible by non-member functions. But if we want to access this data,the first
alternate is to change the accessibility scope from “private” to “public” , but it goes against
the concept of data encapsulation.The best alternate to access private data of a class is the
use of “friend” function. A friend function that is a "friend" of a given class; is allowed to
access private and protected data in that class.Normally, a function that is defined outside of
a class cannot access such information.
Friend function syntax:
friend<return_type> function_name(<arguments_list>);
Example: friend void printWidth( Box box );
Features:
 Friend functions are not a member of a class, but can access data of a class.
 It can be declared or defined in any accessibility mode (public/private/protected)
 No scope resolution operator (::) is required to define a friend function.
 A friend function does not require any class’s object to call.
 We can access private data element as well as private member function using “friend”
keyword.
Accessing private data element
Ex:
#include<iostream.h>
#include<conio.h>
class test
{
private:
int a;
friend void f2(test) ; //it is used to access the private data//
public:
void f1();
};
void test::f1()
{
r function"<<endl;
}
void f2(test obj)
{
cout<<"friend function"<<endl;
obj.a=100; //obj.a is used assign a value to a
cout<<"value="<<obj.a<<endl;
}
void main()
{
clrscr();
test x;
x.f1();
f2(x); /*here object of class test is passed because in the function
definition type of the argument is also the object of the class*/
getch();}
Output:
other function
friend function
value=100

static keyword
A class has two types of members: static & non-static . Static members are class members
and does not require any object to be called where as non static members are instance
members that requires an object of class to be called. “static” keyword is used to declare a
data member or method to static.
static data members : static data members are common to all objects of a class. It is used to
hold the common values that can be shared by different objects. It can be declared in private
or public scope. When a static member is declared in private scope, the non member
functions cannot access it. But, a public static member can be accessed by non members also.
Example:
#include<iostream.h>
#include<conio.h>
class test
{
public:
static int a;
void show();
};
int test::a;
void test::show()
{
cout<<"value of a="<<a;
}
void main()
{
clrscr();
test x;
x.show();
getch();}

Output:
value of a=0
Static variable is initialized to zero by default. It is mandatory to define static variable outside
class using scope resolution operator(::).we can also assign any value during definition like:
int test::a=150;
Static member function : Just as the data member is static; member functions can also be
static. Static member variables are initialized to zero when the first object of its class is
created. Only one initialization is permitted. All other initializations are ignored by the
compiler & only one copy of that member is shared by all. Static member functions can
access static data only.
Ex:
#include<iostream.h>
#include<conio.h>
class counter
{
private:
static int count;
public:
counter()
{
++count;
}
static void show();
};
int counter::count;
void counter::show()
{
cout<<count;
}
void main()
{
clrscr();
cout<<"number of objects before calling :";
counter::show();
counter c1,c2,c3,c4,c5;
cout<<"\nnumber of objects after calling :";
counter::show();
getch();
}
Output:
number of objects before calling : 0
number of objects after calling : 5
class “counter” contains static variable named “count” which is initialized to 0 by default.The
class also contains a default constructor which is incremented by 1 during each call.So,it
counts the number of objects created in whole program because static variable retain the
value.
Inline functions
Inline functions are actual functions, which are copied everywhere during compilation, like
preprocessor macro, so the overhead of function calling is reduced. All the functions defined
inside class definition are by default inline, but you can also make any non-class function
inline by using keyword inline with them. Generally, we prefer to contain only a few lines of
code in inline functions.
For an inline function, the declaration and definition must be done together. For example :
inline void function_name(int a)
{
return a++;
}
An inline function is a combination of macro & function. At the time of declaration or
definition, function name is preceded by word inline.
When inline functions are used, the overhead of a function call is eliminated. Instead, the
executable statements of the function are copied at the place of each function call. This is
done by the compiler.
Ex:
#include <iostream.h>
#include<conio.h>
inline int sqr(int x)
{
int y;
y = x * x;
return y;
}
void main()
{
clrscr();
int a =3, b;
b = sqr(a);
cout<<b;
getch();
}
Here, the statement b = sqr(a) is a function call to sqr(). But since we have declared it as
inline, the compiler replaces the statement with the executable statement of the function (b =
a *a).
Please note that, inline is a request to the compiler. If it is a very complicated function, the
compiler may not be able to convert it to inline. Then it will remain as it is. E.g. Recursive
function, a function containing static variable, function containing return statement or loop or
goto or switch statements are not made inline even if we declare them so.
A function definition in a class definition is an inline function definition, even without the use
of the inline specifier.

Das könnte Ihnen auch gefallen