3 - CPP
3 - CPP
CLASS DECLARATION
Properties of constructor:-
• Constructor has no return type.
• Constructor get called only once in the lifetime of an object whenever it
created.
• Constructor can be overloaded.
• Constructor can be written in private section also.
• Constructor name should have same as class name.
• Constructor can take arguments.
Destructors:-
• In programs, memory of object de allocated after the end of
execution. But the memory allocated dynamically remains in the
RAM until the reboot of computers. To avoid these destructor is
used.
• A destructor is also a special member function as its name is similar
to that of class name but preceded by tild (~) operator.
• It is used to destroy the memory created by constructor.
Properties of constructor:-
• Destructor has no return type.
• Destructor get implicitly called for an object before destroying that
object from the memory.
• Destructor contain clean-up code.
• Destructor can not called explicitly.
Destructors (contd..):-
• Example :-
#include<iostream.h> void main()
#include<conio.h> {
int count=0; clrscr();
class ABC {
{ ABC a;
public: {
ABC() cout<<“First Block”<<endl;
{ ABC b;
count++; {
cout<<“object cout<<“Second Block”<<endl;
created=“<<count<<endl; ABC c;
} }
~ABC() }
{ }
cout<<“object getch();
destroyed=“<<count<<endl; }
count--;
}
};
Operator Overloading:-
• Enhancing the capacity of operator to perform a same task with the same
meaning but for the objects is known as operator overloading.
• Only arithmetic operator can be overloaded.
• Operator overloading is of two types as –
Unary operator overloading
Binary operator overloading
• Syntax :- return_type operator op (arg);
where, return_type - types of return value.
operator - Keyword
op - symbol of operator
arg - refers to arguments
• Some rules to perform operator overloading are –
• Only the existing operator can be overloaded. We can not create new
operator.
• Meaning and performing task of operator should not be changed.
• Operator overloading can also possible using friend function.
• During operator overloading without friend function we pass no argument
for unary operator and one argument for binary operator. Using friend
function, one argument for unary operator and two for binary operator.
INHERITANCE:-
• The mechanism of creating a new class from existing one is called
inheritance.
• The old class or existing class is known as base class or parent class
• The newly created class is known as derived class or child class.
• By inheritance it is possible to reuse the data of existing class into
new class.
• It is also known as polymorphism.
• There are two forms of information as –
• Private inheritance :- during private inheritance the public
section of base class goes to the private section of derived class.
• Public inheritance :- during public inheritance the public section
of base class goes to the public section of derived class.
• There are five types of inheritance as –
i. Single ii. Multilevel iii. Multiple iv. Hybrid v. Hierarchical
Single Inheritance:-
• Single inheritance occur when there is one base class and one derived
class.
Multilevel Inheritance:-
• When there is one base class and one derived class, which is further derived by
another derived class and so on, is known as multilevel inheritance.
Multiple Inheritance:-
• When there is more than one base class and one derived class at that time such a
concept is known as multiple inheritance.
Hybrid Inheritance:-
• When there is presence of more than one types of inheritance in the program,
such a concept is known as hybrid inheritance.
Hierarchical Inheritance:-
• When there is presence of hierarchy of inheritance of various type at that time
such a concept is known as hierarchical inheritance.
-:POLYMORPHISM:-
• Polymorphism is a Greek word means the ability to take more than
one form.
• Polymorphism plays an important role in allowing objects having
different internal structures to share the same external interface.
• Polymorphism is extensively used in implementing inheritance.
• Polymorphism is categorized into following two categories as –
• Compile time polymorphism
• Run time polymorphism
• Compile time polymorphism is always termed as early binding or
static binding. The example is function overloading & operator
overloading.
• Run time polymorphism is always termed as late binding or
dynamic binding. The example is virtual function.
-:Virtual Function:-
• When we use the same function name in both the
base as well as derived class, the function in base
class is declared as virtual by using the keyword
virtual.
• File opening method in C++:- In C++ file can be opened in following two way-
• By using constructor.
• By using open() function.
• By using constructor we can open only one file at a time. But if there is
necessity to open more than one file at a time, at that time we use open
function.
• Following is the syntax for opening a file using constructor.
file_stream_class stream_object (file_name);
• Where, file_stream_class can be ifstream or ofstream.
• stream_object can be any valid identifier.
• file_name is the name of the file to be opened.
-:File opening modes:-
• File opening modes are the parameter which is used with the open function to
specify certain additional task while a file is opened.
• Following are the list of file opening modes along with their meaning.
ios :: app :- Append to end of file.
ios :: ate :- Go to end of file on opening.
ios :: binary :- open file in binary mode
ios :: in :- open file in read only mode.
ios :: out :- Open file in writing mode.
ios :: nocreate :- Open fails if the file does not exist.
ios :: noreplace :- Open fails if the file already exist.
ios :: trunc :- Delete content of the file if it exist.
• For ifstream, the default mode is “ios :: in” and for ofstream the default mode is
“ios :: out”.
• It is also possible to combine two or more modes using bitwise OR operator as –
fout.open(“ABC.CPP”,ios :: out | ios :: nocreate);
-:File Pointers and Their Manipulation:-
• Each file has two pointers, one is input pointer (get pointer) and another is output
pointer (put pointer).
• Input pointer is used for reading the contents of given file location and output
pointer is used for writing to a given file location.
• Thus we can use these pointers to move through the file while reading or writing.
• To move file pointers to any desire position, file stream classes support following
functions as –
seekg() :- Moves get pointer (input) to a specified location.
seekp() :- Moves put pointer (output) to a specified location.
tellg() :- Gives the current position of the get pointer.
tellp() :- Gives the current position of the put pointer.
• E.g.
1. fin.seekg(10); :- Moves file pointer to byte no. 10 starting from zero (0).
2. ofstream fout; fout.open(“ABC.CPP”,ios::app); int n=fout.tellp();
Here n will represent number of bytes in file.
3. seekg(offset, reposition); & seekp(offset, reposition);
‘offset’ represent number of bytes to be moved by the file pointer from the
position specified by ‘reposition’.