Unit 2 Lec Notes
Unit 2 Lec Notes
‘ ’
<<” ”<<
<<” ”<<
•S
•
<<“ ”<<
<<” ”<<
<<” ”<<
<<” ”<<
<<” ”
<<” ”
<<” ”<<
<<” ”<<
<<” ”
<<” ”
<<”_”
<<” ”<<
<<” ”<<” ”
”11’ ”
’ ”
<<” ”
<<” ”
<<” ”;
<<” me”
<<” ”<<e
<<” ”
<<” ”<<
<<” ”<<
<<” ”<<
<<” ”
<<” ”
<<” ”
<<” ”
<<” ”
“ ”<<
<<” ”
<<” ”
<<” 1 :add an item’’
<<” ”
<<” ”
<<” ”
<<” ”
<<” ”
<<” ”
<<” ”
<<” “
<<” ”<<
<<” ”
<<” ”<<
<<” ”
<<” ”
<<” ”
CLASSES WITHIN CLASSES (NESTED CLASS)
A class declared as a member of another class is called as a nested class or a class within another class. The
name of a nested class is local to the enclosing class. The nested class is in the scope of its enclosing class.
Note that simply declaring a class nested in another does not mean that the enclosing class contains an object of
the enclosed class. Nesting expresses scoping, not containment of such objects.For example, a nested class
declaration is shown in the following program segments.
Example 1
class student_info {
private :
long int rollno;
char sex;
public :
student_info(long int rn,char sx);
void display();
class date {
private :
int day;
int month;
int year;
public :
date (int dy, int mh, int yr);
void show_date();
}; // end of date class declaration
};// end of student_info class declaration
Member functions of a nested class have no special access to members of an enclosing class; they object the
usual access rules. Member functions of an enclosing class have no special access to member of a nested class;
they obey the usual access rules. The following program segment illustrates how the member functions of the
nested classes are accessed.
class outer {
int a;
void outer_funt(int b);
class inner {
int x;
void inner_funt(int y);
}; };
outer obj1; // creating an object for the outer class
outer::inner::obj2; //creating an object for the inner class
obj1.outer_funt(); // accessing an outer class member function
obj2.inner.funct(); // accessing of inner class member function
When a class is declared as a member of another class, it contains only the scoping of the outer class. The object
of an outer class does not contain the object of the inner class. The member function of the outer class can be
defined as.
void outer::outer_funt(int b);
{
// methods
}
The member function of the inner class is de ned as.
void outer :: inner:: inner_funt( int y);
{
// methods
}
The following declaration is an invalid way of calling an inner member function of the nested class.
outer obj1; // creating an object for the outer class
outer:: inner::obj2;//creating an object for the inner class
obj1::obj2::inner_funct(); // error
Nesting expresses only scoping for the inner class, not the containment of such object.
Example: A program to defi ne an array of nested class „student_info‟ which contains data members such as
name, roll number and sex, and also consists of one more class „date‟, whose data members are day, month and
year. Again, the class is defi ned with one more class „age_class‟ whose data member is age. The values of the
student_info are read from the keyboard and contents of the class have to be displayed on the screen. This
program shows how to create an array of nested class and their objects, and also how to access these data
members and member functions in C++.
cin >> n;
cout << “ enter the following inoformation \n”;
for (i=0; i<= n-1; ++i) {
int j = i+1;
cout << “ \n object : ” << j << endl;
obj1[i].getbase();
obj2[i].getdate();
obj3[i].getage();
}
cout << “ Contents of the array of nested classes \n”;
cout << “ ------------------------------------------”;
cout << endl;
cout << “ student‟s name Roll_no sex date of birth age”;
cout << endl;
cout << “ ------------------------------------------------”;
cout << endl;
for (i=0; i<= n-1; ++i) {
obj1[i].display();
obj2[i].show_date();
obj3[i].show_age();
}
cout << “-------------------------------------------------”;
cout << endl;
return 0;
}
Local Classes
A class declared inside a function is known as a local class in C++ as it is local to that function. An example of
a local class is given as follows.
#include<iostream.h>
void func()
{
class LocalClass
{
};
}
int main() {
return 0;
}
In the above example, func() is a function and class LocalClass is defined inside the function. So, it is known as
a local class.
A local class name can only be used in its function and not outside it. Also, the methods of a local class must be
defined inside it only. A local class cannot have static data members but it can have static functions.
Example:
#include<iostream.h>
void func() {
class LocalClass {
private:
int num;
public:
void getdata( int n) {
num = n;
}
void putdata() {
cout<<"The number is "<<num;
} };
LocalClass obj;
obj.getdata(7);
obj.putdata();
}
int main() {
cout<<"Demonstration of a local class"<<endl;
func();
return 0;
}
Inline Function
An inline function in C++ is an optimisation feature that reduces the execution time of a program, just as we
have macros in C. This concept of inline functions is used while handling Classes in C++. It is used otherwise as well,
though. Whenever such a function is encountered, the compiler replaces it with a copy of that function‟s code (function
definition). In other words, one may say that such a function is expanded in-line, hence named as an inline function.
However, inlining a function is just a request or suggestion to the compiler, not any mandatory command. It depends on
the compiler whether to accept or decline this suggestion of inlining a particular function. The compiler is most likely
to not consider the inlining of a function under certain circumstances that are mentioned here as follows-
When a function with a return statement is not returning any value and marked as inline, the compiler does not
respond to the programmer‟s request of making it an inline function.
When a programmer tries to inline a function containing a loop (for, while, or do-while), the compiler refuses
the inline suggestion.
When a function is recursive, it cannot be inlined.
A function containing static variables cannot be made an inline function.
The compiler declines the request of inlining a function if it contains any switch or go-to statements.
Here are a few essential points necessary to be remembered about the inline functions.
Inline functions are only suggestions, not compulsions. It is possible that the compiler does not inline the
functions marked as inline by the programmer. Simultaneously, the compiler can decide to inline a function
even if it is not marked as inline.
Functions that are declared and defined inside a class need not be defined as inline functions explicitly since
they are already treated as inline functions by default.
Unlike pre-processor macros, Inline is easy to debug and does not corrupt the namespaces, code as it behaves
as a compiler controlled copy & paste action and not implemented forcibly.
Except for the conditions where the object‟s type is known to the compiler, i.e. when the object has been
declared and constructed inside the same function body, virtual methods cannot be inlined.
The presence of a method or function used as a template in the header section does not make it automatically
inlined.
Example: A program to illustrate an inline member function to read a data member of a class from the keyboard
and to display it on the screen.
//inline demonstration
#include <iostream>
using namespace std;
class sample {
private :
int x;
public :
void getdata();
void display();
};
Due to code expansion, the size of the binary executable program is increased.
Any change in the inline function code would need you to recompile the program to ensure it is updated.
An increase in the page fault leads to poor program performance due to its increased executable size.
For the systems with a large executable code, the call and return overhead time of its functions is negligible
compared to their whole execution time. But, inlining the functions of such a program can bring down the
speed of the system. So, it might not turn out to be a good option always, like in the case of large embedded
systems which prefer high speed over sizeable executable size.
FRIEND FUNCTIONS
The main concepts of the object-oriented programming paradigm are data hiding and data encapsulation.
Whenever data variables are declared in a private category of a class, these members are restricted from
accessing by non-member functions. The private data values can be neither read nor written by nonmember
functions. If any attempt is made directly to access these members, the compiler will display an error message
as “inaccessible data type”. The best way to access a private data member by a non-member function is to
change a private data member to a public group. When the private or protected data member is changed to a
public category, it violates the whole concept of data hiding and data encapsulation. To solve this problem, a
friend function can be declared to have access to these data members. Friend is a special mechanism for letting
non-member functions access private data. A friend function may be either declared or defined within the scope
of a class definition. The keyword friend inform the compiler that it is not a member function of the class. If the
friend function is declared within the class, it must be defined outside the class, but should not be repeated the
keyword friend. The general syntax of the friend function
where friend is a keyword used as a function modifier. A friend declaration is valid only within or outside the
class definition.
I. The following is a valid program segment shows how a friend function is defi ned within the scope
of a class definition
class alpha {
private:
int x;
public :
void getdata();
friend void display (alpha abc)
{
cout << “ value of x = ” << abc.x;
cout << endl;
}
}; // end of class dentition
II. The following program segment shows how a friend function is defi ned out of the class defi nition
class alpha {
private:
int x;
public :
void getdata();
friend void display (alpha abc);
}; // end of class definition
1. Accessing Private Data by Non-Member Function through Friend : The private data members are
available only to the particular class and not to any other part of the program. A non-member function
cannot access these private data. It is now understood that C++ language is not just an enhanced version
of C or the one which introduces only classes and objects.
In case, the keyword struct is used for declaring a class object, all its members are public by default.
There is no data hiding between data members and the outside world. The friend function is a special
type of function which is used to access the private data of any class. In other words, they are defined as
nonmember functions with the ability to manipulate data members directly or to call function members
that are not part of the public interface. The friend class has the right to access as many members of its
class. Each time a friend function accesses the private data, naturally the level of privacy of the data
encapsulation gets reduced. Only if it is necessary to access the private data by non-member functions,
then a class may have a friend function, otherwise it is not necessary
Example1. A program to access the private data of a class by non-member function through
friend, where the friend function is declared in the location of public category.
//declaring friend function
#include <iostream.h>
class sample {
private :
int x;
public :
void getdata();
friend void display(class sample);
};
void sample :: getdata()
{
cout << “ enter a value for x \n”;
cin >> x;
}
void display(class sample abc)
{
cout << “ Entered number is :” << abc.x;
cout << endl;
}
int main()
{
sample obj;
obj.getdata();
cout <<“ accessing the private data by non-member function \n”;
display(obj);
return 0;
}
Example2. A program to access the private data of a class by non-member function through friend,
where the friend function is declared in the location of private category
int x;
friend void display(class sample);
public :
void getdata();
};
void sample :: getdata()
{
cout << “ enter a value for x \n”;
cin >> x;
}
void display(class sample abc)
{
cout << “ Entered number is :” << abc.x;
cout << endl;
}
int main()
{
sample obj;
obj.getdata();
cout <<“ accessing the private data by non-member function \n”;
display(obj);
return 0;
}
2. We can declare a class member function as a friend function of another class
#include <iostream.h>
class TWO; // forward declaration of class TWO
class ONE{
public:
void fun(TWO); // member function declaration
};
class TWO{
private:
int value; // private field
public:
friend void ONE::fun(TWO); // friend function declaration
};
void ONE::fun(TWO two) // function definition
{ two.value=10; // accessing private field
cout<<two.value<<endl;
}
int main(){
ONE one;
TWO two;
one.fun(two);
return 0;
}
3. Granting Friendship to Another Class : A class can have friendship with another class. For
example, let there be two classes, first and second. If the class first grants its friendship with the other
class second, then the private data members of the class first are permitted to be accessed by the public
members of the class second. But on the other hand, the public member functions of the class first
cannot access the private members of the class second.
4. Two Classes Having the Same Friend: A non-member function may have friendship with one or more
classes. When a function has declared to have friendship with more than one class, the friend classes
should have forward declaration. It implies that it needs to access the private members of both classes.
The general syntax of declaring the same friend function with more than one class is,
int y;
public :
inline void getdata();
inline void display();
friend int sum( rst,second);
};
inline void rst :: getdata()
{
cout << “ enter a value for x \n”;
cin >> x;
}
inline void second :: getdata()
{
cout << “ enter a value for y \n”;
cin >> y;
}
inline void rst :: display()
{
cout << “ entered number is (x) = “;
cout << x << endl;
}
inline void second :: display()
{
cout << “ entered number is (y) = “;
cout << y << endl;
}
int sum ( rst one, second two)
{
int temp;
temp = one.x+two.y;
return(temp);
}
int main()
{
rst a;
second b;
a.getdata();
b.getdata();
a.display();
b.display();
int te = sum(a,b);
cout << “ sum of the two private data variables (x+y)”;
cout << “ = “ << te << endl;
return 0;
}
Swapping Private Data in a class
class-2;
class-1
{
<<” ”<<