II Unit Notes
II Unit Notes
#include <iostream>
using namespace std;
class abc
{
public:
static int i;
};
//static data members initializations
int abc::i = 1;
int main()
{
abc obj;
// prints value of i
cout <<"Value of i is "<< obj.i;
}
Output:
Value of i is 1
Static member function
A static member function is a special member function, which is used
to access only static data members, any other normal data member
cannot be accessed through static member function.
Static member function can be accessed with class name, by using
following syntax:
class_name:: function_name(parameter);
Example:
#include <iostream>
using namespace std;
class abc
{
private:
//static data members
static int X;
static int Y;
public:
//static member function
static void Print()
{
cout <<"Value of X: " << X << endl;
cout <<"Value of Y: " << Y << endl;
}
};
//static data members initializations
int abc :: X =10;
int abc :: Y =20;
int main()
{
abc OB;
//accessing member function with object name
cout<<"Printing through object name:"<<endl;
OB.Print();
//accessing member function with class name
cout<<"Printing through class name:"<<endl;
abc::Print();
}
Output:
Printing through object name:
Value of X: 10
Value of Y: 20
Printing through class name:
Value of X: 10
Value of Y: 20
The non-const function nonFunc() can modify member data alpha, but
the constant function conFunc() can’t. If it tries to, a compiler gives
error.
Member functions that do nothing but acquire data from an object are
obvious candidates for being made const, because they don’t need to
modify any data.
Making a function const helps the compiler flag errors, and tells
anyone looking at the listing that you intended the function not to
modify anything in its object.
Advantages of Abstraction:
• Implementation details of the class are protected from the
inadvertent user level errors.
• A programmer does not need to write the low-level code.
• Data Abstraction avoids the code duplication, i.e., programmer does
not have to undergo the same tasks every time to perform the similar
operation.
• The main aim of the data abstraction is to reuse the code and the
proper partitioning of the code across the classes.
• Internal implementation can be changed without affecting the user
level code.
Encapsulation in C++
In normal terms Encapsulation is defined as wrapping up of data and
information under a single unit.
In Object Oriented Programming, Encapsulation is defined as binding
together the data and the functions that manipulates them.
Encapsulation leads to data abstraction or hiding.
Information hiding
Information or data hiding is a programming concept which protects the
data from direct modification by other parts of the program.
The feature of information hiding is applied using Class in most of the
programming languages.
Example
class student
{
char name[30];
int marks;
public:
void display(); };
int main()
{
student S;
S.marks=50; // Error as marks is private
}
Dynamic creation and destruction of objects
C++ supports dynamic memory allocation/deallocation. C++ allocates
memory and initializes the member variables.
An object can be created at run time; such an object is called a dynamic
object.
The construction and destruction of dynamic object is explicitly done by the
programmer.
The new and delete operators are used to allocate and deallocate memory to
such objects.