Scope resolution operator
The :: operator links a class name with a member name in order
to tell the compiler which class the member belongs to.
It can also be used to access a global identifier, which
exactly matches the name of the local identifier.
Ex: #include <iostream>
using namespace std;
int i=120;
int main()
{
int i=100;
cout<<i<<endl; //100
cout<<::i<<endl; //120
return 0;
}
Inline Functions
Each time a function is called, a significant amount of
overhead is generated by the calling and return mechanism.
Actual-parameters are pushed onto the stack and various
registers are saved when a function is called, and then
restored when the function returns. These are time consuming
tasks.
1
In C++ the function code can be expanded at the point where it
is called, termed as inline functions.
When a function is expanded in-line, none of those overhead
operations occur.
In-line function can produce faster run times, it can also
result in larger code size because of duplicated code.
It is best to inline functions for very small functions, less
in statements.
To cause a function to be expanded in line rather than called,
precede its definition with the inline keyword.
2
Like the register keyword, inline is actually just a request,
not a command, to the compiler. The compiler can choose to
ignore it. If a function cannot be inlined, it will simply be
called as a normal function.
Introducing C++ Classes
Purpose of Object Oriented Programming is to achieve DATA
SECURITY.
Class is a user-defined data type. A class is similar
syntactically to a structure.
Class contains within it variables (data members) and
functions (member functions), termed as encapsulation.
Data-members hold information and Member-functions are there
to modify the Data-members in a logical manner.
In class construct data-members usually will be hidden inside
class scope and member-functions are allowed to be accessed
outside the class. Member-functions in turn will access
data-members.
In order to achieve data security, three access specifiers are
used in C++
1. private 2. protected 3. public
These access specifiers must be used within class/structure
constructs in C++.
There are 3 types of scopes in C++.
1. Global scope 2. Local scope 3. Class scope
private access specifier provides class scope for DM / MF
public access specifier provides global scope for DM / MF
protected access specifier is similar to private.
3
The general form of a simple class declaration is
class class-name {
private:
private data and functions
public:
public data and functions
};
Ex:A complex number is made up of real and imaginary values,
which can be accepted and displayed suitably.
# include <iostream>
using namespace std;
class complex
{
private: int r, i;
public:
void accept( )
{
cin>>r>>i;
}
void display( )
{
cout<<r<<"+i"<<i;
}
}; // End of class
int main( ) {
complex c1;
c1.accept( );
c1.display( ); }
“c1” is a variable/object of type complex, also called, an
instance of complex.
No memory will be allocated for class type in C++. Memory
will be allocated to c1 based on the data members
4
No data-member can be declared as auto, extern or register.
Defining member functions outside the class
Member functions of a class can be defined either inside
the class or outside class.
If a member function is defined inside the class it will be
considered as an inline-function.
If a member function is defined outside the class, it will
be considered as a normal function.
Syntax to define member function outside the class
<return-type> <class-name>::<function-name> (Formal
Parameter
list)
{
…..
}
‘::’ is termed as scope-resolution operator in C++.
# include <iostream>
using namespace std;
class complex
{
private: int r, i;
public:
void accept( );
void display( ); // Declaration of member functions
};
5
void complex::accept( )
{
cin>>r>>i;
}
void complex::display( )
{
cout<<r<<"+i"<<i;
}
int main( ) {
complex c1;
c1.accept( );
c1.display( ); }
If no access specifier is associated with DM / MF’s then by
default it is private.
If a member-function is defined inside the class, by default it
is considered as an inline-function.
Program to accept student information and to print it.
# include <iostream>
using namespace std;
# include <string.h>
class student
{
char nm[20];
char usn[20];
int marks[3];
public:
void Accept( );
void Assign(char *, char *, char * );
void Print( ); // Declaration of member functions
};
void student::Accept( )
{
cin>>nm; cin>>usn; cin>>marks[0];
cin>>marks[1]; cin>>marks[2];
6
}
void student::Assign(char *n, char *u, char *m )
{
strcpy(nm,n);
memcpy(usn,u,strlen(u)+1);
// +1 to copy null value too.
// header file for memcpy is <string.h>
sscanf(m,"%d%d%d", &marks[0],&marks[1],&marks[2] );
}
void student::Print( )
{
cout<<"Name : "<<nm<<endl;
cout<<"USN : "<<usn<<endl;
cout<<"Marks:["<<marks[0]<<","<<marks[1]<<","<<marks[2]<<"]";
}
int main( ) {
student s1;
s1.Assign("abc","145","10 8 9") // s1.Accept( );
s1.Print();
}
Structures and classes are related
Structure and class are quite similar.
Only difference is that by default members inside the
structure are public.
In class, by default members inside classes are private.
‘this’ pointer
Member functions are part of the class. Hence, they can
access private data-members.
Memory for data members are not allocated when class is
declared.
Memory for data members is allocated when an object is
created.
7
Usually, objects are not created inside the class, but
outside the class. Only when objects are encountered
during execution time, memory will be allocated to all the
data members which are part of the object.
Scope of object and scope of member-function are totally
different.
Objects contain data-members and cannot be accessed
directly, if objects are defined outside the class.
(Reason is they are usually under private access
specifier.)
In order to access data-members, member-functions must be
called.
Each member function in C++ has an implicit, invisible
pointer termed as ‘this’ pointer.
Type of ‘this’ will be the type of class to which it
belongs.
Task of ‘this’ is to hold the address of the object which
calls the member function.
Ex: s1.Print( );
s1 is termed as invoking object.
s1 is the object which calls Print( ) member-function.
Since, Print( ) is a member-function, it has a ‘this’
pointer.
Whenever a member of a class (i.e DM /MF) is accessed
inside member-function, by default each member of the class
will be prefixed by “this->” automatically by the compiler.
Programmers can prefix each access of the member of the
class by “this->” if needed.
8
Program to demonstrate ‘this’ holds the address of the
invoking object.
# include <iostream>
using namespace std;
class complex
{
private: int r, i;
public:
void access( );
};
void complex::access( )
{
cout<<"\nContent of \"this\" is "<<this<<endl;
this->r = 10;
cout<<this->r;
}
int main( ) {
complex c1;
cout<<"Address of c1 is "<<&c1;
c1.access( );
}