0% found this document useful (0 votes)
10 views51 pages

C++ Notes

The document provides an overview of programming in C++, focusing on object-oriented programming principles, data types, and functions. Key concepts include classes and objects, data abstraction, encapsulation, inheritance, polymorphism, and memory management. Additionally, it covers variable initialization, data types, operators, and function calling methods such as call by value and call by reference.

Uploaded by

Parth Panchal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views51 pages

C++ Notes

The document provides an overview of programming in C++, focusing on object-oriented programming principles, data types, and functions. Key concepts include classes and objects, data abstraction, encapsulation, inheritance, polymorphism, and memory management. Additionally, it covers variable initialization, data types, operators, and function calling methods such as call by value and call by reference.

Uploaded by

Parth Panchal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming in C++ [3330702] Prepared by: Dhara Sharma

[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

For example, int a = 5;


Float: To define float numbers this data type is used. The keyword used for it is
float. The precision for float value after a decimal point is up to 6 digits. The
size to store a float value in memory is 4 bytes.
For example, float a = 12.000000;
Double: To define float values with double precision this data type is used. The
keyword used for it is double. The precision for a double value after a decimal
point is up to 14 digits. The size to store a double value in memory is 8 bytes.
For example, double a = 12.00000000000000;
2. User Defined Data types: structure, union, class, enum
Structure: Structure is a collection of different types of elements. It enables us
to define a new datatype. Elements declared in the structure are known as
structure members. To access those members we need to define a structure
variable and using dot ( . ) Operator with structure variable we can access them.
Structure is defined before the main function.
Syntax: struct structure_name
{
datatype variable;
datatype variable:
.
.
.
}structure_variable;
For example, struct student
{
int rno,age;

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

Syntax: class class_name


{
private : // data member
// member function
public : // data member
// member function
}object ;
For example,
#include<iostream.h>
#include<conio.h>
class demo
{
private : int a; // data member
public : void set() // member function
{
a=10;
}
void show()
{
cout<<"a="<<a;
}
};
void main()
{
demo d; // object declaration
[Link](); // function calling

7
Programming in C++ [3330702] Prepared by: Dhara Sharma

[Link]();
getch();
}
Output: a=10

3. Derived Datatypes: pointers, arrays and functions.


Array: Array is a collection of similar type of elements placed in a contiguous
memory locations.
Syntax: datatype array_name[size];
For example, int a[5];
Here int is a data type, "a" is an array variable and in square brackets [] size of
an array is defined which shows number of elements can be stored in an array.
First element will be stored in a[0] always. Then second in a[1] then a[2], a[3],
a[4]. So the size will be from 0 to n-1 in [ ].
Pointers: Pointer is a variable that holds a memory address of another variable.
If we dynamically want to access the value of a variable stored in some memory
location at that time pointer is used.
Syntax: datatype *pointer_variable;
For example, int a, *ptr;
ptr = &a;
Here ptr is a pointer variable of integer type that holds the address of another
integer variable a. ptr with * sign points to the value stored on that location.

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

1.4.2 Memory Management Operator


The memory management operators are used to manage the memory. There are two
types of memory management operators used in C++ program.
1) New: To allocate the memory to an object or variable at run time.
Syntax: datatype pointer_variable = new datatype;
Example: int *ptr=new int;
2) Delete: To free the memory allocated to object or variable.
Syntax: delete pointer_variable;
Example: delete ptr;
For Example,
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *p=new int;
*p=5;
cout<<“Value =”<<*p<<endl;
cout<<“Address =”<<p;
delete p;
getch();
}
Output:
Value=5
Address=0x8f8f0daa

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

CHAPTER-2: FUNCTIONS IN C++ AND WORKING WITH OBJECTS

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

Use of Default and Constant Argument,


#include<iostream.h>
#include<conio.h>
void sum(int a, const int b=5)
{
cout<<”a+b=”<<a+b;
}
void main()
{
int x=10,y;
sum(x);
getch();
}
Output: a+b=15
2.5 Function Overloading:
When in a program many functions are defined with same name but each
performs different task that is called function overloading. Return type can be
same but the types of arguments passed or number of arguments passed in each
function must be different to overload a function. This is one way in which the
C++ language implements polymorphism.
For example,
#include<iostream.h>
#include<conio.h>
void show(int x)
{
cout<<”value=”<<x<<endl;

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.

Class can be inherited into another Doesn’t support the inheritance


class. concept.
Class provides data hiding by using Structure does not provide data hiding.
private, public and protected
specifiers for members.

16
Programming in C++ [3330702] Prepared by: Dhara Sharma

Note: Read remaining Topics from pg no-63 to 76 from [Link] Atul


Prakashan
2.7 Private and Public Member Functions:
Public Member functions: Member functions defined in the public mode of a
class are public member functions, which can be accessed from anywhere in the
program.
Private Member functions: Member functions defined in the private mode of a
class are private member functions, which can’t be accessed outside the class.
They can be used by member functions only.
For example,
#include<iostream.h>
#include<conio.h>
class demo
{
private: int a;
void get() // private member function
{
a=5;
}
public: void put() // public member function
{
get(); // calling private member function
cout<<”a=”<<a;
}
};

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)

2.9 Static Data Member and Static Member Function:


Note: Read theory portion from [Link] Atul Prakashan (Page No – 81,83)
Example,
#include<iostream.h>
#include<conio.h>
class demo
{
static int a; // declaration of static data member
public: static void modify() //definition of static member function
{
a++;
}
void show()
{
cout<<”value of a=”<<a<<endl;
}

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

2.11 Passing Objects as an argument and Returning Objects:


Note: Read theory portion from [Link] Atul Prakashan (Page No – 89)
For Example,
#include<iostream.h>
#include<conio.h>
class demo
{
int a;
public: void get()
{
cout<<”enter value:”;
cin>>a;
}
demo add(demo p,demo q)// passing objects as an argument
{

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();
}

Output: enter value: 5


enter value: 10
sum=15
2.12 Friend Function and Friend Class:
Friend Function: A friend function is a function that can access the private
members of the class though it is not a member function of that class. To declare
function as a friend of the class we have to write “friend” keyword before the

22
Programming in C++ [3330702] Prepared by: Dhara Sharma

return type in function declaration. Function can be declared in public as well as


private section of class.
Note: characteristics of friend function read from the book (pg no-94,95)
For example,
#include<iostream.h>
#include<conio.h>
class demo
{
int a;
public: void get()
{
a=10;
}
friend void put(demo d) // defining friend function
{
cout<<”a=”<<d.a;
}
};
void main()
{
demo d1;
[Link]();
put(d1); // calling friend function
getch();
}
Output: a=10

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

cout<<”Enter and display details of students”;


for(i=0;i<3;i++)
{
s->get(); // using pointer to array of objects
(*s).put(); // using pointer to array of objects
s++; // navigating objects
}
getch();
}
Output: Enter and display details of students:
Enter rno: 1
Enter name: abc
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

27
Programming in C++ [3330702] Prepared by: Dhara Sharma

CHAPTER-3: CONSTRUCTOR AND DESTRUCTOR

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

3.2 Multiple Constructor(Constructor Overloading):


#include<iostream.h>
#include<conio.h>
class demo
{
int a;
public: demo() // default constructor defined
{
a=10;
}
demo(int i) // parameterized constructor defined
{
a=i;
}
void put()
{
cout<<”a=”<<a;
}
};
void main()
{
demo d1,d2(5); //declaration of objects
[Link]();
[Link]();
getch();

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

class demo2:public demo1


{
public: void display()
{
cout<<”Hi..”<<endl;
}
};
class demo3: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;
d.demo2::show();
d.demo3::show();

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

CHAPTER-5: POINTERS, VIRTUAL FUNCTIONS AND POLYMORPHISM

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

Output: address of an object is 0x8f9dfff4


address of an object is 0x8f9dfff2
address of an object is 0x8f9dfff0
5.4 Return object using ’this’ pointer:
#include<iostream.h>
#include<conio.h>
class demo
{
int a;
public: void get()
{
cout<<”Enter value:”;
cin>>a;
}
demo max(demo ob)
{
If(this->a >= ob.a)
return *this;
else
return ob;
}
void show()
{
cout<<”max=”<<a;
}
};

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

q->show(); // show function of class B called


getch();
}
Output: CLASS A
CLASS A
CLASS B
5.6 Virtual Functions:
Virtual function is a run time polymorphism and it requires pointer to object. To
declare virtual function we only need to write a keyword ‘virtual’ before the
return type in function header. When same function names are used in both base
and derived classes, virtual will be written in only the function of base class. So
when virtual functions are used in a program and if you assign an address of an
object of derived class to the pointer of base class it will call the function defined
in derived class and if you assign an address of an object of base class to the
pointer of base class then it will call the function defined in base class.
#include<iostream.h>
#include<conio.h>
class A
{
public: virtual void show()
{
cout<<”CLASS A”<<endl;
}
};
class B:public A
{

49
Programming in C++ [3330702] Prepared by: Dhara Sharma

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 B called
getch();
}
Output: CLASS A
CLASS B

50
Programming in C++ [3330702] Prepared by: Dhara Sharma

CHAPTER-6 MANAGING CONSOLE I/O OPERATIONS

Note: Read all the topics from [Link]


6.3 Formatted I/O Operations
Example,
#include<iostream.h>
#include<conio.h>
void main()
{
int a=1234;
float b=123.34;
[Link](6);
[Link](‘*’);
cout<<a<<endl;
[Link](1);
cout<<b<<endl;
getch();
}
Output: **1234
123.3

51

You might also like