C++ Notes
C++ Notes
C++ Notes
computational steps to be carried out. Any given procedure might be called at any point
during a program's execution, including by other procedures or itself.
Scoping is another technique that helps keep procedures modular. It prevents the
procedure from accessing the variables of other procedures (and vice versa).
The design method used in procedural programming is called Top Down Design. This
is where you start with a problem (procedure) and then systematically break the problem
down into sub problems (sub procedures). This is called functional decomposition,
which continues until a sub problem is straightforward enough to be solved by the
corresponding sub procedure.
When changes are made to the main procedure (top), those changes can cascade to the
sub procedures of main, and the sub-sub procedures and so on, where the change may
impact all procedures in the pyramid.
In C, a data member must be declared GLOBAL in order to make it accessible by all
functions in the program.
What happens when 2 or more functions work on the same data member?
If there are 10 functions in a program, all these 10 functions can access a global data
member. It is possible one function may accidentally change values of this global data
member. If this data member is a key element of the program, any such accidental
manipulation will affect the whole program. It will be too difficult to debug & identify which
function is causing the problem if the program is really big.
Advantages of Procedural Programming:
Its relative simplicity, and ease of implementation of compilers and interpreters.
The ability to re-use the same code at many places in the program without copying it.
An easier way to keep track of program flow.
Disadvantages of Procedural Programming:
Data is exposed to whole program, so no security for data.
Difficult to relate with real world objects.
Difficult to create new data types reduces extensibility.
Importance is given to the operation on data rather than the data.
Characteristics of Procedural Oriented Programming
1. Emphasis is on procedure.
2. Large programs are divided into smaller programs know as functions.
3. Most of the functions share global data.
4. Data moves openly around from function to function.
5. Functions transform data from one form to another.
6. Employs top-down approach in program design
OBJECT ORIENTED PROGRAMMING:
It is an approach that provides a way of modularizing programs by creating
partitioned memory are area for both data and functions that can be used as
templates for creating copies of such module on demand,
Large programs are divided into smaller Programs are divided into objects.
programs know as functions.
Most of the functions share global data. Data is hidden and cannot be accessed by
external functions.
DATA ENCAPSULATION
1. The wrapping up of data and functions together into single unit is known as
Encapsulation.
2. Data is not accessible by external functions, and accessible only to functions
that are wrapped inside the class.
3. The member functions provide the interface between the Objects data and the
program.
4. The insulation of the data from direct access by the external function is called
DATA HIDING.
DATA ABSTRACTION:
1. The act of representing essential features without including the details is known
as abstraction.
2. Class use the concept of abstraction and are defined as list of abstract attributes
and functions that operate on these attributes.
3. The attributes are known as data members.
4. The functions that operate on data members are called member functions.
PRIVATE PUBLIC
B GET_A
ADD MEMBER
MEMBER FUNCTION
FUNCTION
FUNCTION CALL
DATA ACCESS
DATA
MAIN
Insertion Operator (<<)
It is used along with keyword “cout” to display values on screen;
EXAMPLE:
int a=5,b=10;
cout<<a<<”\t”<<b<<”\n”;
cout<<”hello”;
Extraction Operator(>>)
REFERENCE VARIABLES
A reference variable provides an alternative name for previously defined variable.
General form:
datatype & reference-name = variable-name
Example:
int total=100;
int &sum=total;
If we make the variable sum a reference to the variable total,
then sum and total can be used interchangeably to represent
that variable.
Note: A reference variable must be initialized at the time of declaration.
It can be used in function call by reference mechanism in C++, because it permits the
manipulation of objects by reference, and eliminates the copying of object parameters.
#include<iostream>
using namespace std ;
int main()
{
int a=10;
void change(int&);
cout<<"\n"<<a;
change(a);
cout<<"\n"<<a;
return 0;
}
NOTE: A major application of scope resolution operator is in the classes to identify the
class to which member function belongs.
RETURN BY REFERENCE
A function can also return by reference
#include<iostream>
using namespace std ;
int m=10;
int main()
{
int & max(int &,int&);
int a=10,b=20;
cout<<a<<" "<<b<<endl;
max(a,b)=-1;
cout<<a<<" "<<b<<endl;
return 0;
}
The function returns reference to x or y. Function call max(a,b) will yield a reference to
a or b depending on their values. This means that function call can appear on the left
hand side of assignment statement.
INLINE FUNCTIONS
An inline function is a function that is expanded in line when it is invoked. That is, the
compiler replaces the function call with the corresponding function code. The inline
functions are defined as follows:
Inline function-header
{
Function body
}
The functions are made inline when they are small enough to be defined in one or two
lines.
Note: The inline keyword merely sends a request, not a command, to the compiler. The
compiler may ignore this request if the function definition is too long or too complicated
and compile the function as a normal function.
Situations where inline expansion may not work:
1. For functions returning values, if a loop, a switch exists.
2. For functions not returning values, if a return statements exists.
Advantage: Inline expansion makes the program run faster because the overhead of a
function call and return is eliminated.
Disadvantage: It makes the program to take up more memory because the statements
that define the inline function are reproduced at each point where the
function is called.
DEFAULT ARGUMENTS
C++ allows us to call function without specifying all its arguments.IN such cases the
function assigns a default value to the parameter which does not have a matching
argument in the function call.
Default values are specified when the function is declared.
A default argument is checked for type at the time of declaration and evaluated at the
time function call.
NOTE: Only trailing arguments can have default values
ADVANTAGES:
i) It is used to add new parameters to the existing functions.
ii) Default arguments can be used to combine similar functions into me.
#include<iostream>
using namespace std;
int main()
{
int sample(int a,int b=50,int c=30);
cout<<sample(10,20,50);
cout<<"\n"<<sample(10,20);
cout<<"\n"<<sample(10);
return 0;
}
FUNCTION OVERLOADING
You can have multiple definitions for the same function name in the same scope. The definition
of the function must differ from each other by the types and/or the number of arguments in the
argument list.
#include<iostream>
using namespace std;
int main()
{
float area (float);
float area(float ,float);
cout<<”\nArea of a circle with radius 5 : “<<area(5);
cout<<”\nArea of a rectangle with length and breath 5 and 3 : “<<area(5,3);
return 0;
}
float area(float x)
{ return(22/7.0*x*x);}
Class: It is a way to bind data and its associated functions together to implement data
encapsulation and Data abstraction.
1. Class Declaration: The class declaration defines the type and scope of its members.
class class_name
{
private :
Variable declarations;
Function declarations;
public :
Variable declarations;
Function declarations;
};
i) The class body contains the declaration of variables and functions. These
functions and variables are collectively called class members.
ii) They are usually grouped under two sections namely private and public.
iii) The keywords private and public are known as visibility labels.
iv) Class members that have been declared as private can be accessed only
from within the class. The public members can be accessed from outside the
class also.
v) The use of keyword private is optional. By default the members of the class
are private.
vi) The variables declared inside the class are known as data members and the
functions inside the class are known as member functions.
vii) Only the member functions can have access to private data members and
functions.
viii) However the public data members and public member function can be
accessed outside the class.
NOTE: by default in structures members are public.
A member function incorporates a membership identity label in the header. This label
tells the compiler “to which class the function belongs to”. Scope of the function is
restricted to the class-name specified in the header line.
i) Several different classes can use the same function name. The
“membership label” will resolve there scope.
ii) Member functions can access the private data of the class.
iii) A member function can call another member function directly without
using the dot operator.
NOTE: We can define a member function outside the class definition and
yet make it inline by just adding the keyword “inline” in the header
line of function definition.
Example:
class student
{
int roll;
public: void input();
};
#include<iostream>
using namespace std ;
class time
{ int hours;
int minutes;
public: void get_time() { cin>>hours>>minutes;}
void display() { cout<<hours<<" "<<minutes;}
void sum(time,time);
};
int main()
{
time x,y,z;
x.get_time();
y.get_time();
z.sum(x,y);
z.display();
return 0;
}
CONSTRUCTORS AND DESTRUCTORS
Constructor: A Constructor is a special member function whose task is to initialize the objects of
its own class. It is special because its name is the same as the class name. The constructor is
invoked whenever an object of its associated class is created.
Characteristics of Constructor:
Example:
class student
{
int roll;
float percent ;
public: student (int a, float b) { cin>>roll>>percent;}
};
int main()
{
student z= student(1000,78.89); /*explicit call to constructor*/
student x(2000, 56.78); /* implicit call to constructor */
.
.
.
.
return 0;
}
COPY CONSTRUCTORS:
A copy constructor is used to declare and initialize an object from another object. The process
of initializing through a copy constructor is known as copy initialization. A reference variable has
been used as an argument to the copy constructor. We cannot pass argument by value to the
copy constructor. When no copy constructor is defined the complier supplies its own copy
constructor.
EXAMPLE:
class sample
{
int a,b;
public: sample() {a=10; b=20;} /* default constructor */
sample( int x, int y=99) {a=x; b=y;} /* parameterized constructor */
sample( sample &p) { a=p.a; b=p.b;} /* copy constructor*/
void display()
{ cout<<a<<” “<<b<<endl;}
};
int main()
{
sample z; /* default constructor called */
sample y(88); /* parameterized constructor called);*/
sample w(77,66); /* parameterized constructor called);*/
sample m(w); /*copy constructor called;*/
z.display();
y.display();
w.display();
m.display();
return 0;
}
DYNAMIC CONSTRUCTORS
The constructor can be used to allocate memory while creating objects which enables the
system to allocate the right amount of memory for each object when the objects are not of
same size.
class sample
{
char *name;
int length;
public:
sample() { length=0; name= (char*) malloc(length+1);}
sample(char *s) { length=strlen(s); name= (char*) malloc(length+1); strcpy(name,s);}
void display() {cout<<name<<endl;}
};
int main()
{
sample s1;
sample s2(“john”);
s1.display();
s2.display();
return 0;
}
DESTRUCTORS:
A destructor is special member function whose name is same as the class name but preceded
by a tilde (~). It is used to destroy the objects that have been created by constructors.
It doesn’t take any arguments nor does it return any value.
It is invoked implicitly by the compiler upon exit from block, function and program.