C++ Notes
C++ Notes
[Link]
COMPUTER ENGINEERING DEPARTMENT [SEMESTER-3]
PROGRAMMING IN C++ [3330702]
CHAPTER-1: PRINCIPLES OF OBJECT ORIENTED PROGRAMMING
1.1 Basic concepts of object oriented programming:
1) Class and Objects: A class is a collection of objects of similar type. It consists
data and functions of objects.
To solve a problem, it is divided into number of objects which hold the properties
(data and functions) defined in the class.
For example, Fruit is a class and apple, mango, banana etc. are the objects of that
class.
2) Data Abstraction: It refers to the act of representing essential features without
including the explanations or we can say it hides the implementation details from
the user.
3) Encapsulation and Data Hiding: The wrapping up of data and functions into
a single unit called object, is known as data encapsulation.
The data is accessible only to the functions, which are wrapped in the class. This
insulation of the data from direct access by the program is called data hiding.
4) Inheritance: Inheritance is the process by which one class inherits the
properties of another class. It is the process of creating a new class, called the
derived class, from the existing class, called the base class. For example, Shape is
a base class then we can define a new class called circle (derived class) having the
properties of base class with additional properties of own.
5) Polymorphism: Polymorphism means "many forms". Using Polymorphism we
can define several operations with the same name that can do different things in
related classes. For example, a class named shape is created and the same function
1
Programming in C++ [3330702] Prepared by: Dhara Sharma
named area is defined for circle, rectangle and triangle in the class, executes
differently.
6) Dynamic Binding: Dynamic binding, means that the association of an object
with property or function occurs during run time, rather than at compile time.
7) Message Passing\: The objects communicate with one another by sending &
receiving messages. Establishing communication among objects is called message
passing. Message Passing involves specifying the name of the object, the name of
the function (message) & the information to be sent.
1.2 Variables: A Variable is named memory location which holds data or value of
some type & it can be changed during run time.
Syntax: datatype variablename; OR datatype variablename = value;
For example, int a; // declaration of a variable
OR int a = 5; // compile time initialization of a variable
Note: A variable must be declared before it is used in the program.
Rules for naming variables:
1) Name of the variable can be a combination of alphabets, digits & underscore
but the first character of its name must be an alphabet or underscore.
2) Variable name cannot include any other special characters or a blank space.
3) A keyword cannot be used as a variable name.
Dynamic Initialization of a variable:
When we assign a value to a variable at the time of its declaration in the
program, it is called compile time initialization of a variable. But when we
initialize a variable with some value or assign a value to a variable at run time is
called dynamic initialization of a variable. The following example shows how
we can dynamically initialize a variable:
2
Programming in C++ [3330702] Prepared by: Dhara Sharma
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a;
cout<<"Enter the value of a"<<endl;
cin>>a; // here we are entering the value of a at runtime
cout<<"The value of a = "<<a;
getch();
}
Output: Enter value of a
5
The value of a=5
1.3 Data Types: A data type defines which type of value or a set of values the
variables hold or defines a function i.e. which type of value is returned by it.
Data types are divided into three categories:
1. Basic or Fundamental Data types
2. User Defined Data types
3. Derived Data types
1. Basic Data types: character, integer, float, double
Character: To define a character this data type is used. The keyword used for it
is char. The size to store a character in memory is 1 byte.
For example, char ch = 'a';
Integer: To define the number or integers the integer data type is used. The
keyword used for it is int. The size to store an integer in memory is 2 bytes.
3
Programming in C++ [3330702] Prepared by: Dhara Sharma
4
Programming in C++ [3330702] Prepared by: Dhara Sharma
char name[10];
}s1;
Or you can declare a structure variable in the main function also. To understand
the concept of a structure see the following program for structure student:
#include<iostream.h>
#include<conio.h>
struct student
{
int rno;
char name[10];
};
void main()
{
struct student s1; // structure variable declaration
cout<<"Enter rno of a student";
cin>>[Link];
cout<<"Enter name of a student";
cin>>[Link];
cout<<"rno="<<[Link]<<endl<<"name="<<[Link];
getch();
}
Output: Enter rno of a student
15
Enter name of a student
abc
rno=15
5
Programming in C++ [3330702] Prepared by: Dhara Sharma
name=abc
Union: Union is same as the structure, collection of different types of elements.
And can be defined same as structure. It also enables us to define a new
datatype. Union variable can be declared also same as of it. And even union
members can be accessed as they can be in structure. The only difference is of
memory allocation to members. In structure separate memory is allocated to all
the structure members where in union a common memory is allocated to each
union member.
Syntax: union union_name
{
datatype variable;
datatype variable;
.
.
.
}union_variable;
For example, union student
{
int rno,age;
char name[10];
}u1;
Class: A class is a collection of objects of similar type. It consists data and
functions of objects. Using those objects we can call the functions of that class.
Data and functions defined in a class are private by default so they cannot be
directly accessed by any function. Data and functions defined in a class are
called data members and member functions respectively.
6
Programming in C++ [3330702] Prepared by: Dhara Sharma
7
Programming in C++ [3330702] Prepared by: Dhara Sharma
[Link]();
getch();
}
Output: a=10
8
Programming in C++ [3330702] Prepared by: Dhara Sharma
1.4 Operators
1.4.1 Scope Resolution Operator
There are two scopes of variables: Local & Global. When the variable is declared
outside the function that is called global variable and when the variable is declared
inside the function it is called local variable.
When the variable with the same name are declared inside and outside the function,
compiler will always give preference to the local variable which is declared inside. So
whenever same name variables are declared in both the scopes and we want to access
the global variable at that time we can use Scope Resolution Operator which is
represented by ::.
For example,
#include<iostream.h>
#include<conio.h>
int a=5;
void main()
{
int a=10;
cout<<”Local a=”<<a<<endl;
cout<<”Global a=”<<::a;
getch();
}
Output:
Local a=10
Global a=5
9
Programming in C++ [3330702] Prepared by: Dhara Sharma
10
Programming in C++ [3330702] Prepared by: Dhara Sharma
1.4.3 Manipulators
Manipulators are used to manipulate or format or change the output displayed on the
screen. To use the manipulators in the program the header file <iomanip.h> is required
to be included.
There are mainly four manipulators:
1) endl: To end the current line
2) setw(n): To set the width of the output
3) setfill(‘character’): To fill the remaining space when width is set
4) setprecision(number): To set the precision for floating point value i.e. up to how
many digits after decimal points you want to print
For Example:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
int a=1234,b=56;
float c=123.432;
cout<<setw(4)<<a<<endl;
cout<<setw(4)<<setfill(‘*’)<<b<<endl;
cout<<setprecision(2)<<c<<endl;
getch();
}
Output: 1234
**56
123.43
11
Programming in C++ [3330702] Prepared by: Dhara Sharma
2.1 Function:
Note: Read from [Link] Atul Prakashan (Page No – 46, 47, 48)
2.2 Call by Reference and Call by Value
Call by Value and Returning a value: In this method, the function does not have
any access to the actual variables in the calling program. The values of actual
parameters are copied to the corresponding formal parameters. Thus, even if the
values of the formal parameters change, the values of actual parameters remain
unaffected and vice versa.
For example,
#include<iostream.h>
#include<conio.h>
void show(int x)
{
cout<<”value=”<<x;
}
void main()
{
int a=5;
show(a);
getch();
}
Output: value=5
Call by Reference: If function should change the actual variables in the calling
program, call by reference method is used. In this, the address is passed as an
12
Programming in C++ [3330702] Prepared by: Dhara Sharma
argument, instead of values. This is done with the help of pointers. Thus, when the
values of the formal parameters change, the values of actual parameters are also
affected and vice versa.
For example,
#include<iostream.h>
#include<conio.h>
void show(int *x)
{
cout<<”value=”<<*x;
}
void main()
{
int a=5;
show(&a);
getch();
}
Output: value=5
2.3 Inline Functions:
Note: Read theory from [Link] Atul Prakashan (Page No – 54, 55)
Example,
#include<iostream.h>
#include<conio.h>
inline int square(int x) //defining inline function
{
return x*x;
}
13
Programming in C++ [3330702] Prepared by: Dhara Sharma
void main()
{
int a=5;
cout<<”value=”<<square(a);
getch();
}
Output: value=25
2.4 Default and Constant Arguments:
Default Argument: We usually pass the values or references in the calling
function. But C++ facilitates with the concept of default argument, in which we
don’t need to pass the values in the calling statement. The default value is given to
the parameter in the function definition. So for that parameter if we omit the value
from calling function, it considers the default value given to it.
Note: Read the rules followed by default argument from [Link] Atul Prakashan
(Pg No-57)
For example,
void test (int a, int b=5, int c=10);
Constant Argument: If you already know that the function is not supposed to
alter the value of parameter, you should declare it as constant by writing a const
keyword before the data type of argument. It increases the execution speed. It
makes the value of that variable constant for that function where it is declared as
const.
For example,
void test (const int a, const int b);
14
Programming in C++ [3330702] Prepared by: Dhara Sharma
15
Programming in C++ [3330702] Prepared by: Dhara Sharma
}
void show(float y)
{
cout<<”value=”<<y<<endl;
}
void main()
{
int a=5;
float b=5.5;
show(a);
show(b);
getch();
}
Output: value=5
value=5.5
2.6 Structure vs. Classes:
CLASS STRUCTURE
All members are private by default in All members are public by default in
class. class.
Class consists different types of data Structure only consists different types
and function. of data not function.
16
Programming in C++ [3330702] Prepared by: Dhara Sharma
17
Programming in C++ [3330702] Prepared by: Dhara Sharma
void main()
{
demo d;
[Link](); // calling public member function
getch();
}
Output: a=5
2.8 Arrays within a Class:
Note: Read from [Link] Atul Prakashan (Page No – 78,79)
18
Programming in C++ [3330702] Prepared by: Dhara Sharma
};
int demo::a=5; // initialization of static data member
void main()
{
demo d1,d2,d3;
[Link]();
demo::modify(); // calling static member function
demo::modify(); // calling static member function
[Link]();
[Link](); // calling static member function using object
[Link]();
getch();
}
Output: value of a=5
value of a=7
value of a=8
2.10 Array of Objects:
Example,
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
char name[20];
public: void get()
{
19
Programming in C++ [3330702] Prepared by: Dhara Sharma
cout<<”Enter rno:”;
cin>>rno;
cout<<”Enter name:”:
cin>>name;
}
void put()
{
cout<<”ROLL NO: ”<<rno<<endl;
cout<<”NAME: ”<<name<<endl;
}
};
void main()
{
student s[3]; // array of 50 objects declared
int i;
cout<<”Enter and display details of students:”<<endl;
for(i=0;i<3;i++)
{
s[i].get(); // using array of objects
s[i].put(); // using array of objects
}
getch();
}
Output: Enter and display details of students:
Enter rno: 1
Enter name: abc
20
Programming in C++ [3330702] Prepared by: Dhara Sharma
ROLL NO: 1
NAME: abc
Enter rno: 2
Enter name: pqr
ROLL NO: 2
NAME: pqr
Enter rno: 3
Enter name: xyz
ROLL NO: 3
NAME: xyz
21
Programming in C++ [3330702] Prepared by: Dhara Sharma
demo temp;
temp.a=p.a+q.a;
return temp; // returning object
}
void show()
{
cout<<”sum=”<<a<<endl;
}
};
void main()
{
demo d1,d2,d3;
[Link]();
[Link]();
d3=[Link](d1,d2);
[Link]();
getch();
}
22
Programming in C++ [3330702] Prepared by: Dhara Sharma
23
Programming in C++ [3330702] Prepared by: Dhara Sharma
Friend Class: All the member functions of one class can be declared as friend
function of another class then the class is called friend class.
For example,
#include<iostream.h>
#include<conio.h>
class demo1
{
int a;
public: void get()
{
a=10;
}
friend class demo2; //declaring class demo2 as a friend of class
demo1
};
class demo2
{
public: void put(demo1 d)
{
cout<<”a=”<<d.a; //accessing private member
of class demo1
}
};
void main()
{
24
Programming in C++ [3330702] Prepared by: Dhara Sharma
demo1 d1;
demo2 d2;
[Link]();
[Link](d1);
getch();
}
Output: a=10
2.13 Pointer to objects:
Note: Read theory portion from book (pg no-99)
For example,
class demo
{
int a;
public: void show()
{
a=15;
cout<<”a=”<<a;
}
};
void main()
{
demo *d=new demo;
d->show();
// you can write it as (*d).show(); also
getch();
}
25
Programming in C++ [3330702] Prepared by: Dhara Sharma
Output: a=15
2.14 Pointer to Array of Object:
Example,
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
char name[20];
public: void get()
{
cout<<”enter rno”;
cin>>rno;
cout<<”enter name”:
cin>>name;
}
void put()
{
cout<<”ROLL NO: ”<<rno;
cout<<”NAME: ”<<name;
}
};
void main()
{
student *s=new student[3]; // array of 50 objects declared
int i;
26
Programming in C++ [3330702] Prepared by: Dhara Sharma
27
Programming in C++ [3330702] Prepared by: Dhara Sharma
NOTE: Read full theory portion from [Link] (Page no from 109 to 124)
3.1 Default Constructor and Destructor:
#include<iostream.h>
#include<conio.h>
class demo
{
int a;
public: demo() // default constructor defined
{
a=5;
}
void put()
{
cout<<”a=”<<a<<endl;
}
~demo() // destructor defined
{
cout<<”Object destroyed”;
}
};
void main()
{
demo d; //declaration of object //constructor called automatically
[Link]();
28
Programming in C++ [3330702] Prepared by: Dhara Sharma
getch();
}
Output: a=5
Object destroyed
3.2 Parameterized Constructor:
#include<iostream.h>
#include<conio.h>
class demo
{
int a;
public: demo(int i) // parameterized constructor defined
{
a=i;
}
void put()
{
cout<<”a=”<<a;
}
};
void main()
{
demo d(5); //declaration of object //constructor called automatically
[Link]();
getch();
}
Output: a=5
29
Programming in C++ [3330702] Prepared by: Dhara Sharma
30
Programming in C++ [3330702] Prepared by: Dhara Sharma
Output: a=10
a=5
3.4 Copy Constructor:
#include<iostream.h>
#include<conio.h>
class demo
{
int a;
public: demo(int i) // parameterized constructor defined
{
a=i;
}
demo(const demo &d) // copy constructor defined
{
a=d.a;
cout<<”a=”<<a<<endl;
cout<<”COPY CONSTRUCTOR CALLED”<<endl;
}
};
void main()
{
demo d1(5); // declaration of object
demo d2(d1); // copy constructor called
getch();
31
Programming in C++ [3330702] Prepared by: Dhara Sharma
}
Output: a=5
COPY CONSTRUCTOR CALLED
3.5 Constructor with Default Argument:
#include<iostream.h>
#include<conio.h>
class demo
{
int a,b;
public: demo(int i,int j=10) // constructor with default argument
{
a=i;
b=j;
}
void put()
{
cout<<”a=”<<a<<endl<<”b=”<<b;
}
};
void main()
{
demo d(5); //constructor use default argument=10
[Link]();
getch();
}
Output: a=5
32
Programming in C++ [3330702] Prepared by: Dhara Sharma
b=10
3.6 Dynamic Constructor:
#include<iostream.h>
#include<conio.h>
class demo
{
int *a;
public: demo(int i)
{
a=new int;
*a=i;
}
void put()
{
cout<<”value=”<<*a;
}
};
void main()
{
demo *d=new demo(5); //object created dynamically dynamic
constructor called
d->put();
getch();
}
Output: value=5
33
Programming in C++ [3330702] Prepared by: Dhara Sharma
CHAPTER-4: INHERITANCE
NOTE: Read full theory portion from [Link] (Page no from 130 to 165)
4.1Single Inheritance:
#include<iostream.h>
#include<conio.h>
class demo1
{
public: void show()
{
cout<<”Hello”<<endl;
}
};
class demo2:public demo1
{
public: void display()
{
cout<<”Hi”<<endl;
}
};
void main()
{
demo2 d;
[Link]();
[Link]();
getch();
34
Programming in C++ [3330702] Prepared by: Dhara Sharma
}
Output: Hello
Hi
4.2 Multiple Inheritances:
#include<iostream.h>
#include<conio.h>
class demo1
{
public: void show()
{
cout<<”Hello ”;
}
};
class demo2
{
public: void put()
{
cout<<’’World!”<<endl;
}
};
class demo3:public demo1,public demo2
{
public: void display()
{
cout<<”Hi..”<<endl;
}
35
Programming in C++ [3330702] Prepared by: Dhara Sharma
};
void main()
{
demo3 d;
[Link]();
[Link]();
[Link]();
getch();
}
Output: Hello World!
Hi..
4.3 Ambiguity in Multiple Inheritances:
#include<iostream.h>
#include<conio.h>
class demo1
{
public: void show()
{
cout<<”Hello ”;
}
};
class demo2
{
public: void show()
{
36
Programming in C++ [3330702] Prepared by: Dhara Sharma
cout<<’’World!”<<endl;
}
};
class demo3:public demo1,public demo2
{
public: void display()
{
cout<<”Hi..”<<endl;
}
};
void main()
{
demo3 d;
d.demo1::show(); //calls show function of class demo1
d.demo2::show(); //calls show function of class demo2
[Link]();
getch();
}
Output: Hello World!
Hi..
4.4 Multilevel Inheritance:
#include<iostream.h>
#include<conio.h>
class demo1
{
public: void show()
37
Programming in C++ [3330702] Prepared by: Dhara Sharma
{
cout<<”Hello”<<endl;
}
};
class demo2:public demo1
{
public: void display()
{
cout<<”Hi..”<<endl;
}
};
class demo3:public demo2
{
public: void put()
{
cout<<”Good Morning!!”<<endl;
}
};
void main()
{
demo3 d;
[Link]();
[Link]();
[Link]();
getch();
}
38
Programming in C++ [3330702] Prepared by: Dhara Sharma
Output: Hello
Hi..
Good Morning!!
4.5 Hierarchical Inheritance:
#include<iostream.h>
#include<conio.h>
class demo1
{
public: void show()
{
cout<<”Hello”<<endl;
}
};
class demo2:public demo1
{
public: void display()
{
cout<<”Hi..”<<endl;
}
};
class demo3:public demo1
{
public: void put()
{
cout<<”Good Morning!!”<<endl;
39
Programming in C++ [3330702] Prepared by: Dhara Sharma
}
};
void main()
{
demo2 d2;
demo3 d3;
[Link]();
[Link]();
[Link]();
[Link]();
getch();
}
Output: Hello
Hi..
Hello
Good Morning!!
4.6 Hybrid Inheritance:
#include<iostream.h>
#include<conio.h>
class demo1
{
public: void show()
{
cout<<”Hello”<<endl;
}
};
40
Programming in C++ [3330702] Prepared by: Dhara Sharma
41
Programming in C++ [3330702] Prepared by: Dhara Sharma
[Link]();
[Link]();
[Link]();
getch();
}
Output: Hello
Hello
Hi..
Good Morning!!
Have a good day!!
4.7 Virtual Base Class:
#include<iostream.h>
#include<conio.h>
class demo1
{
public: void show()
{
cout<<”Hello”<<endl;
}
};
class demo2:virtual public demo1
{
public: void display()
{
cout<<”Hi..”<<endl;
}
42
Programming in C++ [3330702] Prepared by: Dhara Sharma
};
class demo3:virtual public demo1
{
public: void put()
{
cout<<”Good Morning!!”<<endl;
}
};
class demo4:public demo2,public demo3
{
public: void print()
{
cout<<”Have a good day!!”<<endl;
}
};
void main()
{
Demo4 d;
[Link]();
[Link]();
[Link]();
[Link]();
getch();
}
Output: Hello
Hi..
43
Programming in C++ [3330702] Prepared by: Dhara Sharma
Good Morning!!
Have a good day!!
4.8 Constructor in Derived Class:
#include<iostream.h>
#include<conio.h>
class demo1
{
public: demo1()
{
cout<<”Base Class constructor called”<<endl;
}
};
class demo2:public demo1
{
public: demo2():demo1()
{
cout<<’’Derived Class constructor called”;
}
};
void main()
{
demo2 d;
getch();
}
Output: Base Class constructor called
Derived Class constructor called
44
Programming in C++ [3330702] Prepared by: Dhara Sharma
NOTE: Read full theory portion from [Link] (Page no from 173 to 193)
5.1 Pointers in C++:
Note: Basic Pointers program to print the address and value of a variable (Refer
my class notes. Theory is already given above in chp-1)
5.2 Develop programs using pointer to object and pointer to Array of objects:
Note: This program is given above in chp-2 topic-2.13 and 2.14
5.3 ’this’ pointer:
#include<iostream.h>
#include<conio.h>
class demo
{
public: void show()
{
cout<<”address of an object is ”<<this<<endl;
}
};
void main()
{
demo d1,d2,d3;
[Link]();
[Link]();
[Link]();
getch();
}
45
Programming in C++ [3330702] Prepared by: Dhara Sharma
46
Programming in C++ [3330702] Prepared by: Dhara Sharma
void main()
{
demo d1,d2,d3;
[Link]();
[Link]();
d3=[Link](d2);
[Link]();
getch();
}
Output: Enter value: 10
Enter value: 15
max=15
5.5 Pointer to derived class:
When the function is overloaded in derived class means when the same function
names and same arguments are used in both base and derived classes, and we take
a base class pointer and assign an address of an object of base class to that pointer
then it will call the function defined in the base class. Even when we assign an
address of an object of derived class to the base class pointer, it will call the
function defined in base class only. It only matches the type of pointer used and
will not consider the address of object assigned to it. One solution is that we can
take the pointer of derived class and assign an address of the object of derived
class, so it will call the function defined in derived class. But the problem with
this is, it could only call the functions which are derived from base class not those
which are member functions of that derived class. To solve this problem virtual
functions are used.
47
Programming in C++ [3330702] Prepared by: Dhara Sharma
#include<iostream.h>
#include<conio.h>
class A
{
public: void show()
{
cout<<”CLASS A”<<endl;
}
};
class B:public A
{
public: void show()
{
cout<<”CLASS B”<<endl;
}
};
void main()
{
A *p,a;
p=&a;
p->show(); // show function of class A called
B b;
p=&b;
p->show(); //show function of class A called-function is overloaded
B *q;
q=&b;
48
Programming in C++ [3330702] Prepared by: Dhara Sharma
49
Programming in C++ [3330702] Prepared by: Dhara Sharma
50
Programming in C++ [3330702] Prepared by: Dhara Sharma
51