You are on page 1of 101

C++ Tutorial

In this tutorial we will give you simple and easy study material related to C++ language, C++ is

object oriented programming language. C++ are mainly used for Design Operating system,

Design Language Compiler, Design Database, Utilities.

History of C++

C++ language is developed by Bjarne Stroustrup in the year 1979 at bell laboratory at USA, C+

+ is a simple and Object Oriented Programming Language.

Prerequisites

Before learning C++ Programming language you need basic knowledge of C programming

language, In C++ all basic concept are inherited from C language.

Overview of C++

C++ is a computer programming language developed in 1980 by Bjarne Stroustrup at the Bell

Telephone Laboratories. C++ is an Object Oriented Programming Language, which follow

oops concept like, inheritance, encapsulation, abstraction and polymorphism.

Where we use C++ Language

C Language is mainly used for;

 Design Operating system

 Design Language Compiler

 Design Database

 Utilities
 Application Software

Features of C++

C++ is object oriented programming language and it is a very simple and easy language, It is the

enhanced form of C programming language. this language have following features and here we

discuss some important features of C++.

Important Features of C++

 Simple

 Portability

 Powerful

 Platform dependent

 Object oriented oriented


 Case sensitive

 Compiler based

 Syntax based language

 Use of Pointers

Simple

Every C++ program can be written in simple English language so that it is very easy to

understand and developed by programmer.

Platform dependent

A language is said to be platform dependent whenever the program is execute in the same

operating system where that was developed and compiled but not run and execute on other

operating system. C++ is platform dependent language.

Note: .obj file of C++ program is platform dependent.

Portability

It is the concept of carrying the instruction from one system to another system. In C++

Language .cpp file contain source code, we can edit also this code. .exe file contain application,
only we can execute this file. When we write and compile any C++ program on window operating

system that program easily run on other window based system.

When we can copy .exe file to any other computer which contain window operating system then it

works properly, because the native code of application an operating system is same.

Powerful

C++ is a very powerful programming language, it have a wide verity of data types, functions,

control statements, decision making statements, etc.

Object oriented Programming language

This main advantage of C++ is, it is object oriented programming language. It follow concept of

oops like polymorphism, inheritance, encapsulation, abstraction.

Case sensitive

C++ is a case sensitive programming language. In C++ programming 'break and BREAK' both

are different.

If any language treats lower case latter separately and upper case latter separately than they can

be called as case sensitive programming language [Example c, c++, java, .net are sensitive
programming languages.] other wise it is called as case insensitive programming language

[Example HTML, SQL is case insensitive programming languages].

Compiler based

C++ is a compiler based programming language that means without compilation no C++ program

can be executed. First we need compiler to compile our program and then execute.

Syntax based language

C++ is a strongly tight syntax based programming language. If any language follow rules and

regulation very strictly known as strongly tight syntax based language. Example C, C++, Java,

.net etc. If any language not follow rules and regulation very strictly known as loosely tight syntax

based language.

Example HTML.

Efficient use of pointers

Pointers is a variable which hold the address of another variable, pointer directly direct access to

memory address of any variable due to this performance of application is improve. In C++

language also concept of pointer are available.

Advantage of C++ Programming Language

 Abstract data type defining is very good

 C++ language is efficient having less compiled time.

 It is much suitable for large projects.

 Encapsulation, polymorphism, abstraction are the important properties of C++


language

 Objects, methods, instance, message passing, inheritance are some important


properties inherited by this language
 C++ Programming is easy to maintain and modify existing code as new objects
with small difference to existing ones.

 C++ Programming is implemented on real life scenario.

 The properties of inheritance make simple the program by complexity. Do not


required to write again.

 Implementation details are hidden from other modules which represent a clearly
defined interface.

Applications of C++

Mainly C++ Language is used for Develop Desktop application and system software. Some

application of C++ language are given below.

 For Develop Graphical related application like computer and mobile games.

 To evaluate any kind of mathematical equation use C++ language.

 C++ Language are also used for design OS. Like window xp.
 Google also use C++ for Indexing

 Few parts of apple OS X are written in C++ programming language.

 Internet browser Firefox are written in C++ programming language

 All major applications of adobe systems are developed in C++ programming


language. Like Photoshop, ImageReady, Illustrator and Adobe Premier.

 Some of the Google applications are also written in C++, including Google file
system and Google Chromium.

 C++ are used for design database like MySQL.

How to Install C++ Editor

Installation of TC is very simple just download turbo C or C++ and run .exe files

When you install the Turbo C or C++ compiler on your system, then TC directory is created on

the hard disk and various sub directories such as INCLUDE, and LIB etc. are created under TC.

 INCLUDE :Contain the header files of C and C++.


 LIB: Contain the library files of C and C++.
 BGI: Contain Graphics related files.
 BIN: Contain .exe, .obj etc files.

Turbo C For Window(xp, 7, 8)

Previously Turbo C are not work properly on Window 7 and its above versions. Here you can get

Turbo C for Window 7 and its above versions.

TC Editor

TC Editor is very simple and easy to use; here i will give you all tips related to TC Editor and

some shortcut keys related to TC Editor which is very useful at the time of coding. Turbo C is a

most common C language compiler. Below i will discuss all about its Interfaces.

TC Editor
The interface of Turbo C is very simple. When IDE screen appears, the menu bar is activated. It

contains various menus such as;

 File: This menu contains group of commands used for save , edit , print program,
exit from Turbo C editor etc.
 Edit: This menu contains group of commands used for editing C program source
code. Example Copy, Cut, Paste, Undo etc.
 Search: This menu contains group of commands used for searching specific
word as well as replacing it with another one.
 Run: This menu contains group of commands used for running C program.
 Compile: This menu contains group of commands used for compiling C
program.
 Debug: This menu contains group of commands used for debugging C program.
 Project: This menu contains group of commands used for opening, closing and
creating projects.
 Options: This menu contains group of commands used for configuring IDE of
Turbo C and setting up directories etc.
 Windows: This menu contains group of commands used for opening, closing
various windows of IDE.
 Help: This menu is used to get help about specific topic of C language. Similarly
to get help about a specific keyword or identifier of C.

Shortcut keys Related to TC Editor

 Alt + x : Close TC Editor.


 Clt + f9 : Run C Program.
 Alt + f9 : Compile C Code.
 Alt + Enter : Get Full Screen or Half Screen TC Editor.
 Clt + y : Delete complete above line of cursor.
 Shift + Right arrow : Select Line of Code.
 Clt + Insert : Copy.
 Shift + Insert : Paste.
 Shift + Delete : Delete.

Turbo C For Window(xp, 7, 8)

Previously Turbo C are not work properly on Window 7 and its above versions. Here you can get

Turbo C for Window 7 and its above versions.

Compiler in C++

A compiler is system software which converts programming language code into binary format in

single steps. In other words Compiler is a system software which can take input from other any

programming language and convert it into lower level machine dependent language.

Interpreter

It is system software which is used to convert programming language code into binary format in

step by step process.

Assembler

An assembler is system software which is used to convert the assembly language instruction into

binary format in step by step process. An assembler is system software which is used to convert

the assembly language instruction into binary format.

Difference Between Compiler Interpreter


No Compiler Interpreter

Compiler takes Entire program as input Interpreter takes Single instruction as


1
at a time. input at a time.

No Intermediate Object code is


2 Intermediate Object code is generated
generated

It execute conditional control It execute conditional control


3
statements fastly. statements slower than Compiler

4 More memory is required. Less memory is required.

Program need not to be compiled every Every time higher level program is
5
time converted into lower level program

It display error after entire program is It display error after each instruction
6
checked interpreted (if any)

7 Example: C, C++ Example: BASIC

Comments in C++

Generally Comments are used to provide the description about the Logic written in program.

Comments are not display on output screen.

When we are used the comments, then that specific part will be ignored by compiler.

In 'C++' language two types of comments are possible

 Single line comments

 Multiple line comments

Single line comments

Single line comments can be provided by using / /....................

Multiple line comments

Multiple line comments can be provided by using /*......................*/


Note: When we are working with the multiple line comments then nested comments are not

possible.

Rules for Writing Comments

1. Program contains any number of comments at any place.

Example

// header files
#include<iostream.h>
#include<conio.h>

void main()
{
// variable declaration
int a,b,c;
a=10;
b=20;
c=a+b;
cout<<"Sum: "<<c;
getch();
}

2. Nested Comments are not possible, that means comments within comments.

Example

void main()
{
/*
/* comments */
*/
}

3. Comments can be splits over more than one line.

Example

void main()
{
/* main
function
body part
*/
}

4. Comments are not case sensitive.

Example

void main()
{
/* MAIN Function BODY */

5. Single line comments start with "//"

Keywords in C++

Keyword is a predefined or reserved word in C++ library with a fixed meaning and used to

perform an internal operation. C++ Language supports more than 64 keywords.

Every Keyword exists in lower case latter like auto, break, case, const, continue, int etc.

32 Keywords in C++ Language which is also availabe in C language.

auto double int struct

break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

do if static while

Another 30 reserved words that were not in C, these are new to C++

asm dynamic_cast namespace reinterpret_cast

bool explicit new static_cast

catch false operator template

class friend private this

const_cast inline public throw


delete mutable protected true

try typeid typename using

using virtual wchar_t

Note: you can not use these keyword for variable and not overload.

Constant in C++

It is an identifier whose value can not be changed at the execution time of program. In

general constant can be used to represent as fixed values in a C++ program. Constants are

classified into following types.

If any single character (alphabet or numeric or special symbol) is enclosed between single cotes '

' known as single character constant.

If set of characters are enclosed between double cotes " " known as string character constant.
Variable in C++

Variable is an identifier which holds data or another one variable is an identifier whose value can

be changed at the execution time of program. Variable is an identifier which can be used to

identify input data in a program.

Syntax

Variable_name = value;

Variable declarations

This is the process of allocating sufficient memory space for the data in term of variable.

Syntax
Datatype variable_name; int a;

If no input values are assigned by the user than system will gives a default value called garbage

value.
Garbage value

Garbage value can be any value given by system and that is no way related to correct programs.

It is a disadvantage and it can overcome using variable initialization.

Variable initialization

It is the process of allocating sufficient memory space with user defined values.

Syntax

Datatype nariable_name=value;

Example

int b = 30;

Variable assignment

It is a process of assigning a value to a variable.

Syntax
Variable_Name = value
Example

int a= 20;

int b;
Example

b = 25; // --> direct assigned variable

b = a; // --> assigned value in term of variable

b = a+15; // --> assigned value as term of expression

Variable Declaration Rule in C++

To Declare any variable in C++ you need to follow rules and regulation of C++ Language, which

is given below;

 Every variable name should start with alphabets or underscore (_).

 No spaces are allowed in variable declaration.


 Except underscore (_) no special symbol are allowed in the middle of the
variable declaration.

 Maximum length of variable is 8 characters depend on compiler and operation


system.
 Every variable name always should exist in the left hand side of assignment
operator.

 No keyword should access variable name.


Note: In a c program variable name always can be used to identify the input or output data.

Variable declarations

This is the process of allocating sufficient memory space for the data in term of variable.

Syntax

Datatype variable_name;

int a;

If no input values are assigned by the user than system will gives a default value called garbage

value.

Scope of Variable in C++

In C++ language, a variable can be either of global or local scope.

Global variable

Global variables are defined outside of all the functions, generally on top of the program. The

global variables will hold their value throughout the life-time of your program.
Local variable

A local variable is declared within the body of a function or a block. Local variable only use within

the function or block where it is declare.

Example of Global and Local variable


Example

#include<iostream.h>
#include<conio.h>

int a; // global variable


void main()
{
int b; // local variable
a=10, b=20;
cout<<"Value of a: "<<a;
cout<<"Value of b: "<<b;
getch();
}

Output

Value of a: 10

Value of b: 20

Datatype in C++

Data type is a keyword used to identify type of data. It is used for storing the input of the program

into the main memory (RAM) of the computer by allocating sufficient amount of memory space in

the main memory of the computer.

In general every programming language is containing three categories of data types. They are
 Fundamental or primitive data types

 Derived data types

 User defined data types

Primitive data types

These are the data types whose variable can hold maximum one value at a time, in C++

language it can be achieve by int, float, double, char.

Example

int a; // valid

a = 10,20,30; // invalid

Derived data types

These data type are derived from fundamental data type. Variables of derived data type allow us

to store multiple values of same type in one variable but never allows to store multiple values of

different types. These are the data type whose variable can hold more than one value of similar

type. In C++ language it can be achieve by array.

Example

int a[] = {10,20,30}; // valid


int b[] = {100, 'A', "ABC"}; // invalid
User defined data types

User defined data types related variables allows us to store multiple values either of same type or

different type or both. This is a data type whose variable can hold more than one value of

dissimilar type, in C++ language it is achieved by structure.

Syntax

struct emp
{
int id;
char ename[10];
float sal;
};

In C++ language, user defined data types can be developed by using struct, union, enum etc.

Datatype Modifiers in C++

In C++ language Data Type Modifiers are keywords used to change the properties of current

properties of data type. Datatype modifiers are classified into following types.

 long

 short

 unsigned

 signed

Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of

storage space allocated to a variable.

For example, storage space for int data type is 4 byte for 32 bit processor. We can increase the

range by using long int which is 8 byte. We can decrease the range by using short int which is 2

byte.

long:
This can be used to increased size of the current data type to 2 more bytes, which can be applied

on int or double data types. For example int occupy 2 byte of memory if we use long with integer

variable then it occupy 4 byte of memory.

Syntax

long a; --> by default which represent long int.

short

In general int data type occupies different memory spaces for a different operating system; to

allocate fixed memory space short keyword can be used.

Syntax

short int a; --> occupies 2 bytes of memory space in every operating system.

unsigned

This keyword can be used to make the accepting values of a data type is positive data type.

Syntax

unsigned int a =100; // right


unsigned int a=-100; // wrong
Signed

This keyword accepts both negative or positive value and this is default properties or data type

modifiers for every data type.

Example

int a=10; // right


int a=-10; // right
signed int a=10;// right
signed int a=-10;` // right

Note: in real time no need to write signed keyword explicitly for any data type.

Operators in C++

Operator is a special symbol that tells the compiler to perform specific mathematical or logical

Operation.

 Arithmetic Operators

 Relational Operators

 Logical Operators

 Bitwise Operators

 Assignment Operators

 Ternary or Conditional Operators


Arithmetic Operators

Given table shows all the Arithmetic operator supported by C Language. Lets suppose

variable Ahold 8 and B hold 3.

Operator Example (int A=8, B=3) Result

+ A+B 11

- A-B 5

* A*B 24

/ A/B 2

% A%4 0

Relational Operators

Which can be used to check the Condition, it always return true or false. Lets suppose

variable Ahold 8 and B hold 3.

Operators Example (int A=8, B=3) Result

< A<B False


<= A<=10 True

> A>B True

>= A<=B False

== A== B False

!= A!=(-4) True

Logical Operator

Which can be used to combine more than one Condition?. Suppose you want to combined two

conditions A<B and B>C, then you need to use Logical Operator like (A<B) && (B>C).

Here &&is Logical Operator.

Operator Example (int A=8, B=3, C=-10) Result

&& (A<B) && (B>C) False

|| (B!=-C) || (A==B) True

! !(B<=-A) True

Truth table of Logical Operator

C1 C2 C1 && C2 C1 || C2 !C1 !C2

T T T T F F

T F F T F T

F T F T T F

F F F F T T

Assignment operators

Which can be used to assign a value to a variable. Lets suppose variable A hold 8 and B hold 3.

Operator Example (int A=8, B=3) Result

+= A+=B or A=A+B 11
-= A-=3 or A=A+3 5

*= A*=7 or A=A*7 56

/= A/=B or A=A/B 2

%= A%=5 or A=A%5 3

=a=b Value of b will be assigned to a

Increment and Decrement Operator in C++

Increment operators are used to increase the value of the variable by one and decrement

operators are used to decrease the value of the variable by one.

Both increment and decrement operator are used on single operand or variable, so it is called as

unary operator. Unary operators are having higher priority than the other operators it means

unary operators are execute before other operators.

Syntax

++ // increment operator

-- // decrement operator

Note: Increment and decrement operators are can not apply on constant.


Example

x= 4++; // gives error, because 4 is constant

Type of Increment Operator

 pre-increment

 post-increment

pre-increment (++ variable)


In pre-increment first increment the value of variable and then used inside the expression

(initialize into another variable).

Syntax

++ variable;

Example pre-increment in C++

#include<iostream.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=++i;
cout<<"x: "<<x;
cout<<"i: "<<i;
getch();
}

Output

x: 11

i: 11

In above program first increase the value of i and then used value of i into expression.

post-increment (variable ++)

In post-increment first value of variable is use in the expression (initialize into another variable)

and then increment the value of variable.

Syntax
variable ++;

Example post-increment

#include<iostream.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=i++;
cout<<"x: "<<x;
cout<<"i: "<<i;
getch();
}

Output

x: 10

i: 11

In above program first used the value of i into expression then increase value of i by 1.

Type of Decrement Operator

 pre-decrement

 post-decrement

Pre-decrement (-- variable)

In pre-decrement first decrement the value of variable and then used inside the expression

(initialize into another variable).

Syntax
-- variable;

Example pre-decrement

#include<iostream.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=--i;
cout<<"x: "<<x;
cout<<"i: "<<i;
getch();
}

Output

x: 9

i: 9

In above program first decrease the value of i and then value of i used in expression.

post-decrement (variable --)

In Post-decrement first value of variable is use in the expression (initialize into another variable)

and then decrement the value of variable.

Syntax

variable --;

Example post-decrement
#include<iostream.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=i--;
cout<<"x: "<<x;
cout<<"i: "<<i;
getch();
}

Output

x: 10

i: 9

In above program first used the value of x in expression then decrease value of i by 1.

Example of increment and decrement operator

Example

#include<iostream.h>
#include<conio.h>

void main()
{
int x,a,b,c;
a = 2;
b = 4;
c = 5;
x = a-- + b++ - ++c;
cout<<"x: "<<x;
getch();
}

Output

x: 0

First C++ Program

For writing C++ code you need turbo c editor and write code after writing code save the code with

.cpp extension, compile the code and finally run the program.

Example

#include<iostream.h>
#include<conio.h>

void main()
{
cout<<"This is my first program";
getch();
}

Output

This is my first program

 After writing complete code save the program using F2


 Save your code with .cpp extension for example: hello.cpp
 After save the code you need to compile your code using alt+f9
 Finally Run the program using clt+f9

Save C program
Save any C++ program using .cpp Extension with file name. For example your program name is

sum, then it save with sum.cpp.


Syntax

filename.cpp

Compile and Run C++ Program

For compile any C++ program you just press alt+f9 , after compilation of your C++ program you

press clt+f9 for run your C++ program.


Syntax

Compile -> alt+f9

Run -> clt+f9

Ternary Operator in C++

If any operator is used on three operands or variable is known as Ternary Operator. It can be

represented with ? : . It is also called as conditional operator

Advantage of Ternary Operator


Using Ternary Operator reduce the number of line codes and improve the performance of

application.

Syntax

expression-1 ? expression-2 : expression-3

In the above symbol expression-1 is condition and expression-2 and expression-3 will be either

value or variable or statement or any mathematical expression. If condition will be true

expression-2 will be execute otherwise expression-3 will be executed.

Syntax

a<b ? cout<<"a is less" : cout<<"a is greater";

Flow Diagram
Find largest number among 3 numbers using ternary operator
Example Ternary Operator in C++

#include<iostream.h>
#include<conio.h>

void main()
{
int a, b, c, large;
clrscr();
cout<<"Enter any three number: ";
cin>>a>>b>>c;
large=a>b ? (a>c?a:c) : (b>c?b:c);
cout<<"Largest Number is: "<<large;
getch();
}

Output

Enter any three number: 5 7 2

Largest number is 7

Expression Evaluation in C++

In c language expression evaluation is mainly depends on priority and associativity.

Priority:

This represents the evaluation of expression starts from "what" operator.

Associativity:

It represents which operator should be evaluated first if an expression is containing more than

one operator with same priority.

Operator Priority Associativity

{}, (), [] 1 Left to right

++, --, ! 2 Right to left

*, /, % 3 Left to right

+, - 4 Left to right

<, <=, >, >=, ==, != 5 Left to right

&& 6 Left to right

|| 7 Left to right
?: 8 Right to left

=, +=, -=, *=, /=, %= 9 Right to left

Example 1:

Example 2:
Types of Error in C++

Error is a abnormal condition whenever it occurs execution of the program is stopped these are

mainly classified into following types.

Types of Error in C++

 Compile time error

 Run time error

Compile Time Error

If any error is generated at the time of compilation is known as compile time error, in general

these are raised while break down the rules and regulation of programming language. Compile

time errors also known as syntax errors.

Compile Time Error Example

Missing semicolon, writing keyword in upper case.


Run time error
If any error is generated at run time is known as runtime error, in general these are raised

because of writing wrong logic in the program.

Example

Calling function without existence, divide by zero.

Int a=10,b;

B=a/0; --> infinite

Here out of range of int data type.

In general it is very difficult to identify logical error in C language, to overcome this problem

exception handling was introduced in object oriented programming language.

Warning
Warning is also an abnormal condition but whenever it occurred execution of program will never

be stopped.

Note: In C language warning can be neglected but error can not be neglected.
Structure of C++ Program

Every C++ program can be written with the following syntax.

Syntax

#include<headerfilename.h> --> include section

class class_name
{
data members;
user defined method;
{
..........
..........
}
};

returntype main() --> main method


{
.........
.........
}

Include section

# include is a pre-processor directive can be used to include all the predefined method of given

header files into current C program before compilation.

Syntax

#include<headerfile.h>

class

Class is a blue print which is containing only list of variables and method.
C++ library is collection of header files, header files is a container which is collection of related

predefined method.

User defined method section

If any method is defined by the user is known as user defined method. Method is collection of

statement used to perform a specific Operation.

Syntax

returntype method_name()

.......

.......

In the above syntax method name can be any user defined name, return type represents which

type of value it can return to its calling method.

Syntax

methodName(); // calling method

Note: User defined method are Optional in a C++ program.

Main() method

This is starting executable block of any program (it is always executed by processor and OS ).

One C++ program can have maximum one main() the entire statements of given program can be

executed through main(). Without main() method no C++ program will be executed.

Syntax
returntype main()

......

.....

If return type is void that method can not return any value to the operating system. So that void

can be treated as no return type.

Example

#include<iostream.h>
#include<conio.h>

void main()
{
cout<<("Hello main";
}

Output

Hello main

IO statements in C++

IO represents input output statements, and input statement can be used to read the input value

from the standard input device (keyboard), output statement can be used to display the output in

standard output device (Monitor) respectively. In C++ language IO statement can be achieve by

using cout<< and >>cin.

cout and cin in C++

cout<<: cout is used for print message on screen


Example

#include<iostream.h>
#include<conio.h>

void main()
{
int num;
clrscr();
cout<<"Hello word !"; // print message on screen
getch();
}

Output

Hello word !

cin

cin>>: cin is used for get or read value form keyboard.


Example

#include<iostream.h>
#include<conio.h>

void main()
{
int num;
clrscr();
cout<<"Enter any number:";
cin>>num; // accept one value form keyboard
cout<<"Number is: "<<num;
getch();
}
Output

Enter any number :100

Number is: 100

main() Function in C++

main() function is the entry point of any C++ program. It is the point at which execution of

program is started. When a C++ program is executed, the execution control goes directly to the

main() function. Every C++ program have a main() function.


Syntax

void main()
{
............
............
}

In above syntax;

 void: is a keyword in C++ language, void means nothing, whenever we use void
as a function return type then that function nothing return. here main() function no
return any value.
 In place of void we can also use int return type of main() function, at that time
main() return integer type value.
 main: is a name of function which is predefined function in C++ library.

Simple example of main()


Example

#include<stdio.h>
void main()
{
cout<<"This is main function";
}

Output

This is main function

Clrscr() and Getch() in C++

clrscr() and getch() both are predefined function in "conio.h" (console input output header file).

Clrscr()

It is a predefined function in "conio.h" (console input output header file) used to clear the console

screen. It is a predefined function, by using this function we can clear the data from console

(Monitor). Using of clrscr() is always optional but it should be place after variable or function

declaration only.

Example of clrscr()

#include<iostream.h>
#include<conio.h>

void main()
{
int a=10, b=20;
int sum=0;
clrscr(); // use clrscr() after variable declaration
sum=a+b;
cout<<"Sum: "<<sum;
getch();
}

Output
Sum: 30

Getch()

It is a predefined function in "conio.h" (console input output header file) will tell to the console wait

for some time until a key is hit given after running of program.

By using this function we can read a character directly from the keyboard. Generally getch() are

placing at end of the program after printing the output on screen.

Example of getch()

#include<iostream.h>
#include<conio.h>

void main()
{
int a=10, b=20;
int sum=0;
clrscr();
sum=a+b;
cout<<"Sum: "<<sum;
getch(); // use getch() befor end of main()
}

Output
Sum: 30
Function in C++

A function is a group of statements that together perform a specific task. Every C++ program has

at least one function, which is main().


Why use function ?

Function are used for divide a large code into module, due to this we can easily debug and

maintain the code. For example if we write a calculator programs at that time we can write every

logic in a separate function (For addition sum(), for subtraction sub()). Any function can be called

many times.

Advantage of Function

 Code Re-usability

 Develop an application in module format.

 Easily to debug the program.


 Code optimization: No need to write lot of code.

Type of Function

There are two type of function in C++ Language. They are;

 Library function or pre-define function.

 User defined function.


Library function

Library functions are those which are predefined in C++ compiler. The implementation part of

pre-defined functions is available in library files that are .lib/.obj files. .lib or .obj files are

contained pre-compiled code. printf(), scanf(), clrscr(), pow() etc. are pre-defined functions.

Limitations of Library function

 All predefined function are contained limited task only that is for what purpose
function is designed for same purpose it should be used.

 As a programmer we do not having any controls on predefined function


implementation part is there in machine readable format.

 In implementation whenever a predefined function is not supporting user


requirement then go for user defined function.

User defined function

These functions are created by programmer according to their requirement for example suppose

you want to create a function for add two number then you create a function with name sum() this

type of function is called user defined function.


Defining a function.

Defining of function is nothing but give body of function that means write logic inside function

body.

Syntax

return_type function_name(parameter)

function body;

 Return type: A function may return a value. The return_type is the data type of
the value the function returns.Return type parameters and returns statement are
optional.
 Function name: Function name is the name of function it is decided by
programmer or you.
 Parameters: This is a value which is pass in function at the time of calling of
function A parameter is like a placeholder. It is optional.
 Function body: Function body is the collection of statements.

Function Declarations

A function declaration is the process of tells the compiler about a function name. The actual body

of the function can be defined separately.

Syntax

return_type function_name(parameter);

Note: At the time of function declaration function must be terminated with ';'.
calling a function.

When we call any function control goes to function body and execute entire code. For call any

function just write name of function and if any parameter is required then pass parameter.

Syntax

function_name();

or

variable=function_name(argument);

Note: At the time of function calling function must be terminated with ';'.
Example of Function in C++

#include<iostream.h>
#include<conio.h>

void sum(); // declaring a function


int a=10,b=20, c;

void sum() // defining function


{
c=a+b;
cout<<"Sum: "<<c;
}
void main()
{
clrsct();
sum(); // calling function
}

Output

Sum: 30
Call by Value and Call by Reference in C++

On the basis of arguments there are two types of function are available in C++ language, they

are;

 With argument

 Without argument

If a function take any arguments, it must declare variables that accept the values as a arguments.

These variables are called the formal parameters of the function. There are two ways to pass

value or data to function in C++ language which is given below;

 call by value

 call by reference
Call by value

In call by value, original value can not be changed or modified. In call by value, when you

passed value to the function it is locally stored by the function parameter in stack memory

location. If you change the value of function parameter, it is changed for the current function only

but it not change the value of variable inside the caller function such as main().
Call by value

#include<iostream.h>
#include<conio.h>

void swap(int a, int b)


{
int temp;
temp=a;
a=b;
b=temp;
}

void main()
{
int a=100, b=200;
clrscr();
swap(a, b); // passing value to function
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
getch();
}

Output

Value of a: 200

Value of b: 100

Call by reference

In call by reference, original value is changed or modified because we pass reference

(address). Here, address of the value is passed in the function, so actual and formal arguments

shares the same address space. Hence, any value changed inside the function, is reflected

inside as well as outside the function.


Example Call by reference

#include<iostream.h>
#include<conio.h>

void swap(int *a, int *b)


{
int temp;
temp=*a;
*a=*b;
*b=temp;
}

void main()
{
int a=100, b=200;
clrscr();
swap(&a, &b); // passing value to function
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
getch();
}

Output

Value of a: 200

Value of b: 100

Difference between call by value and call by reference.

call by value call by reference

This method copy original value into This method copy address of arguments
1
function as a arguments. into function as a arguments.

Changes made to the parameter inside Changes made to the parameter affect
2 the function have no effect on the the argument. Because address is used
argument. to access the actual argument.

Actual and formal arguments will be Actual and formal arguments will be
3
created in different memory location created in same memory location

Note: By default, C++ uses call by value to pass arguments.

Inline Function in C++

Inline Function is powerful concept in C++ programming language. If a function is inline, the

compiler places a copy of the code of that function at each point where the function is called at

compile time.

To make any function inline function just preceded that function with inline keyword.
Why use Inline function

Whenever we call any function many time then, it take a lot of extra time in execution of series of

instructions such as saving the register, pushing arguments, returning to calling function. For

solve this problem in C++ introduce inline function.

Advantage of Inline Function

The main advantage of inline function is it make the program faster.

Syntax

inline function_name()

//function body

Example

#include<iostream.h>
#include<conio.h>

inline void show()


{
cout<<"Hello world";
}

void main()
{
show(); // Call it like a normal function
getch();
}

Output
Hello word

Where inline function not work ?

 If inline function are recursive

 If function contain static variables.

 If return statement are exits but not return any value.

Function Overloading in C++

Whenever same method name is exiting multiple times in the same class with different number of

parameter or different order of parameters or different types of parameters is known as method

overloading.

Why method Overloading

Suppose we have to perform addition of given number but there can be any number of

arguments, if we write method such as a(int, int)for two arguments, b(int, int, int) for three

arguments then it is very difficult for you and other programmer to understand purpose or

behaviors of method they can not identify purpose of method. So we use method overloading to

easily figure out the program. For example above two methods we can write sum(int, int) and

sum(int, int, int) using method overloading concept.

Syntax

class class_Name

Returntype method()

{
...........

...........

Returntype method(datatype1 variable1)

...........

...........

Returntype method(datatype1 variable1, datatype2 variable2)

...........

...........

};

Different ways to overload the method

There are two ways to overload the method in C++

 By changing number of arguments or parameters

 By changing the data type

By changing number of arguments

In this example, we have created two overloaded methods, first sum method performs addition of

two numbers and second sum method performs addition of three numbers.

Program Function Overloading in C++


#include<iostream.h>
#include<conio.h>

class Addition
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(int a, int b, int c)
{
cout<<a+b+c;
}
};
void main()
{
clrscr();
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30);
}

Output

30

60

By changing the data type

In this example, we have created two overloaded methods that differs in data type. The first sum

method receives two integer arguments and second sum method receives two float arguments.

Function Overloading Program in C++


#include<iostream.h>
#include<conio.h>

class Addition
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(float a, float b)
{
cout<<a+b+c;
}
};
void main()
{
clrscr();
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30);
}

Output

30

25.25

Note: The scope of overloading is within the class.

Friend Function in C++


In C++ a Friend Function that is a "friend" of a given class is allowed access to private and

protected data in that class.

A function can be made a friend function using keyword friend. Any friend function is preceded

with friend keyword. The declaration of friend function should be made inside the body of class

(can be anywhere inside class either in private or public section) starting with keyword friend.

Why use friend function ?

You do not access private or protected data member of any class, to access private and

protected data member of any class you need a friend function.

Syntax

class class_name

......

friend returntype function_name(arguments);

Friend class

Similarly like, friend function a class can be made a friend of another class using keyword friend.

Syntax

class A

friend class B; // class B is a friend class

......

}
class B

......

When a class is made a friend class, all the member functions of that class becomes friend

function.

If B is declared friend class of A then, all member functions of class B can access private and

protected data of class A but, member functions of class A can not private and protected data of

class B.

Note: Remember, friendship relation in C++ is always granted not taken.

Example Friend function

In below example you can access private function of class employee by using friend function.

Example

#include<iostream.h>
#include<conio.h>

class employee
{
private:
friend void sal();
};

void sal()
{
int salary=4000;
cout<<"Salary: "<<salary;
}
void main()
{
employee e;
sal();
getch();
}

Output

Salary: 4000

Virtual Function in C++

A virtual function is a member function of class that is declared within a base class and re-

defined in derived class.

When you want to use same function name in both the base and derived class, then the function

in base class is declared as virtual by using the virtual keyword and again re-defined this

function in derived class without using virtual keyword.


Syntax

virtual return_type function_name()

.......

.......

Virtual Function Example


#include<iostream.h>
#include<conio.h>

class A
{
public:
virtual void show()
{
cout<<"Hello base class";
}
};

class B : public A
{
public:
void show()
{
cout<<"Hello derive class";
}
};

void main()
{
clrsct();
A aobj;
B bobj;
A *bptr;
bptr=&aobj;
bptr->show(); // call base class function

bptr=&bobj;
bptr->show(); // call derive class function
getch();
}

Output
Hello base class

Hello derive class

Recursion in C++

When method is call within same method is called Recursion. The method which call same

method is called recursive method. In other words when a method call itself then that method is

called Recursive method.

Recursive method are very useful to solve many mathematical problems like to calculate factorial

of a number, generating Fibonacci series, etc.

Advantage of Recursion

 method calling related information will be maintained by recursion.

 Stack evaluation will be take place by using recursion.

 In fix prefix, post-fix notation will be evaluated by using recursion.

Disadvantage of Recursion

 It is a very slow process due to stack overlapping.

 Recursive programs can create stack overflow.

 Recursive method can create as loops.

Find the Factorial of any number using recursion


Example Recursion in C++

#include<iostream.h>
#include<conio.h>
void main()
{
int fact(int);
int i,f,num;
clrscr();
cout<<"Enter any number: ";
cin>>num;
f=fact(num);
cout<<"Factorial: "<<f;
getch();
}

int fact(int n)
{
if(a<0)
return(-1);
if(a==0)
return(1);
else
{
return(n*fact(n-1));
}
}

Output

Enter any number: 5

Factorial: 120
Find the Table of any number using recursion
Example

#include<iostream.h>
#include<conio.h>

void main()
{
int table(int,int);
int n,i; // local variable
clrscr();
cout<<"Enter any num : ";
cin>>n;
for(i=1;i< =10;i++)
{
cout<<n<<"*"<<i<<= <<table(n,i)<<endl;
}
getch();
}
int table(n,i)
{
int t;
if(i==1)
{
return(n);
}
else
{
t=(table(n,i-1)+n);
return(t);
//return(table(n,i-1)+n);
}
}

Output

Enter any number: 5

5*1= 5

5*2= 10

5*3= 15

5*4= 20

5*5= 25

5*6= 30

5*7= 35

5*8= 40

5*9= 45

5*10= 50

Oops Concept in C++


The main purpose of C++ programming was to add object orientation to the C programming

language, which is in itself one of the most powerful programming languages. If any programming

language follow below oops concept then that language called object oriented programming

language.

 Object

 Class

 Encapsulation

 Abstraction

 Inheritance

 Polymorphism

Object

Object is the physical as well as logical entity where as class is the only logical entity.

Class
Class: Class is a blue print which is containing only list of variables and method and no memory

is allocated for them. A class is a group of objects that has common properties.

Encapsulation

Encapsulation is a process of wrapping of data and methods in a single unit is called

encapsulation. Encapsulation is achieved in C++ language by class concept. The main

advantage of using of encapsulation is to secure the data from other methods, when we make a

data private then these data only use within the class, but these data not accessible outside the

class.

Abstraction

Abstraction is the concept of exposing only the required essential characteristics and behavior

with respect to a context.

Hiding of data is known as data abstraction. In object oriented programming language this is

implemented automatically while writing the code in the form of class and object.

Inheritance

The process of obtaining the data members and methods from one class to another class is

known as inheritance. It is one of the fundamental features of object-oriented programming.

Polymorphism

The process of representing one Form in multiple forms is known as Polymorphism. Here one

form represent original form or original method always resides in base class and multiple forms

represents overridden method which resides in derived classes.

Note: All these concept I will discuss in detail later.


Access Specifiers in C++

Access specifiers in C++ define how the members of the class can be accessed. C++ has 3

new keywords introduced, namely.

 public

 private

 protected

The keywords public, private, and protected are called access specifiers. A class can have

multiple public, protected, or private labeled sections.

Note: By default, all members and function of a class is private i.e if no access specifier is

specified.

Syntax of Declaring Access Specifiers in C++


Syntax

class
{

private:

// private members and function

public:

// public members and function

protected:

// protected members and function

};

Public Access Specifier in C++

Public class members are accessible out side the class and it is available for every one.

Syntax

class Public_Access_Specifier

public: // public access specifier

int a; // Data Member Declaration

void display(); // Member Function declaration

Private Access Specifier in C++

Private class members are accessible with the class and it is not accessible out side the class. If

some one try to access out side the it gives compile time error. By default class variables and

member functions are private.


Syntax

class Private_Access_Specifier

private: // private access specifier

int a; // Data Member Declaration

void display(); // Member Function declaration

Private and Public Access Specifier Example in C++


Example

#include<iostream.h>
#include<conio.h>

class A
{
private:
int a;
public:
int b;

public:

void show()
{
a=10 ;
b=20;
clrscr();
//Every members can be access here, same class
cout<<"\nAccessing variable within the class"<<endl;
cout<<"Value of a: "<<a<<endl;
cout<<"Value of b: "<<b<<endl;
}
};

void main()
{
A obj; // create object
obj.show();

cout<<"\nAccessing variable outside the class"<<endl;


//'a' cannot be accessed as it is private
//cout<<"value of a: "<<obj.a<<endl;

//'b' is public as can be accessed from any where


cout<<"value of b: "<<obj.b<<endl;

getch();
}

Note: If here, we access variable a in side main method it will give compile time error
Output

Accessing variable within the class

value of a: 10

value of b: 20

value of c: 30

Accessing variable outside the class

Value of b: 20
Protected Access Specifier in C++

It is similar to private access specifier. It makes class member inaccessible outside the class. But

they can be accessed by any subclass of that class.

Syntax

class Protected_Access_Specifier

protected: // protected access specifier

int a; // Data Member Declaration

void display(); // Member Function Declaration

Access Specifier Example in C++

In below example I will show you all these access specifier public, private and protected.

Access Specifier Example in C++

#include<iostream.h>
#include<conio.h>

//using namespace std;

class Declaration
{
private:
int a;
public:
int b;
protected:
int c;
public:

void show()
{
a=10;
b=20;
c=30;

//Every members can be access here, same class


cout<<"\nAccessing variable within the class"<<endl;

cout<<"Value of a: "<<a<<endl;
cout<<"Value of b: "<<b<<endl;
cout<<"Value of c: "<<c<<endl;
}
};

class Sub_class:public Declaration


{
public:
void show()
{
b=5;
c=6;
cout<<"\nAccessing variable in sub the class"<<endl;

// a is not accessible here it is private


//cout<<"Value of a: "<<a<<endl;
//b is public so it is accessible any where
cout<<"Value of b: "<<b<<endl;
//'c' is declared as protected, so it is accessible in sub class
cout<<"Value of c: "<<c<<endl;
}
};
void main()
{
clrscr();
Declaration d; // create object
d.show();

Sub_class s; // create object


s.show(); // Sub class show() function

cout<<"\nAccessing variable outside the class"<<endl;


//'a' cannot be accessed as it is private
//cout<<"value of a: "<<d.a<<endl;

//'b' is public as can be accessed from any where


cout<<"value of b: "<<d.b<<endl;

//'c' is protected and cannot be accesed here


//cout<<"value of c: "<<d.c<<endl;
getch();
}

Output

Accessing variable within the class

value of a: 10

value of b: 20

value of c: 30

Accessing variable in sub class

value of b: 5

value of c: 6
Accessing variable outside the class

Value of b: 20

Class and Object in C++

Object is the physical as well as logical entity where as class is the only logical entity.

Class: Class is a blue print which is containing only list of variables and method and no memory

is allocated for them. A class is a group of objects that has common properties.

A class in C++ contains, following properties;

 Data Member

 Method

 Constructor

 Block

 Class and Interface


Object: Object is a instance of class, object has state and behaviors.

An Object in C++ has three characteristics:

 State

 Behavior

 Identity
Syntax

class_name object_reference;

Example
Employee e;

State: Represents data (value) of an object.

Behavior: Represents the behavior (functionality) of an object such as deposit, withdraw etc.

Identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible

to the external user. But,it is used internally by the JVM to identify each object uniquely.

Class is also can be used to achieve user defined data types.

In real world many examples of object and class like dog, cat, and cow are belong to animal's

class. Each object has state and behaviors. For example a dog has state:- color, name, height,

age as well as behaviors:- barking, eating, and sleeping.

Vehicle class

Car, bike, truck these all are belongs to vehicle class. These Objects have also different different

states and behaviors. For Example car has state - color, name, model, speed, Mileage. as we;;

as behaviors - distance travel


Difference between Class and Object in C++

Class Object

Class is a container which collection of


1 object is a instance of class
variables and methods.

Sufficient memory space will be allocated


No memory is allocated at the time of
2 for all the variables of class at the time of
declaration
declaration.

One class definition should exist only For one class multiple objects can be
3
once in the program. created.

Syntax to declare a Class


Syntax

class Class_Name

data member;

method;
}

Simple Example of Object and Class

In this example, we have created a Employee class that have two data members eid and ename.

We are creating the object of the Employee class and printing the objects value.

Example

#include<iostream.h>
#include<conio.h>

class Employee
{
public:
int salary // data member
void sal()
{
cout<<"Enter salary: ";
cin>>salary;
cout<<"Salary: "<<salary;
}
};
void main()
{
clrscr();
Employee e; //creating an object of Employee
e.sal();
getch();
}

Output

Enter salary: 4500

Salary: 4500
Inheritance in C++

The process of obtaining the data members and methods from one class to another class is

known as inheritance. It is one of the fundamental features of object-oriented programming.

Important points

 In the inheritance the class which is give data members and methods is known as
base or super or parent class.

 The class which is taking the data members and methods is known as sub or
derived or child class.
Syntax

class subclass_name : superclass_name

// data members

// methods

Real Life Example of Inheritance in C++

The real life example of inheritance is child and parents, all the properties of father are inherited

by his son.
Diagram

In the above diagram data members and methods are represented in broken line are inherited

from faculty class and they are visible in student class logically.

Advantage of inheritance

If we develop any application using this concept than that application have following advantages,

 Application development time is less.


 Application take less memory.

 Application execution time is less.

 Application performance is enhance (improved).

 Redundancy (repetition) of the code is reduced or minimized so that we get


consistence results and less storage cost.

Tpyes of Inheritance

Based on number of ways inheriting the feature of base class into derived class it have five types

they are:

 Single inheritance

 Multiple inheritance

 Hierarchical inheritance

 Multiple inheritance

 Hybrid inheritance

Single inheritance

In single inheritance there exists single base class and single derived class.

Multiple inheritances

In multiple inheritances there exists single base class, single derived class and multiple

intermediate base classes.


Single base class + single derived class + multiple intermediate base classes.

Intermediate base classes

An intermediate base class is one in one context with access derived class and in another

context same class access base class.

Hence all the above three inheritance types are supported by both classes and interfaces.

Multiple inheritance

In multiple inheritance there exist multiple classes and singel derived class.
Hybrid inheritance

Combination of any inheritance type

Inheriting the feature from base class to derived class

In order to inherit the feature of base class into derived class we use the following syntax

Syntax

class classname-2 : classname-1


{

variable declaration;

method declaration;

Explanation

 classname-1 and classname-2 represents name of the base and derived classes
respectively.
 : is operator which is used for inheriting the features of base class into derived
class it improves the functionality of derived class.
Example of Inheritance in C++

#include<iostream.h>
#include<conio.h>

class employee
{
public:
int salary;
};
class developer : public employee
{
employee e;
public:
void salary()
{
cout<<"Enter employee salary: ";
cin>>e.salary; // access base class data member
cout<<"Employee salary: "<<e.salary;
}
};
void main()
{
clrscr();
developer obj;
obj.salary();
getch();
}

Output

Enter employee salary: 50000

Employee salary: 50000

Abstraction in C++

Abstraction is the concept of exposing only the required essential characteristics and behavior

with respect to a context.

Hiding of data is known as data abstraction. In object oriented programming language this is

implemented automatically while writing the code in the form of class and object.

Real life example of Abstraction in C++

Abstraction shows only important things to the user and hides the internal details for example

when we ride a bick, we only know about how to ride bick but can not know about how it work ?

and also we do not know internal functionality of bick.


Example of Abstraction in C++

#include<iostream.h>
#include<conio.h>

class sum
{
// hidden data from outside world
private: int a,b,c;

public:
void add()
{
clrscr();
cout<<"Enter any two numbers: ";
cin>>a>>b;
c=a+b;
cout<<"Sum: "<<c;
}
};
void main()
{
sum s;
s.add();
getch();
}

Output
Enter any two number:

Sum: 9

Note: Data abstraction can be used to provide security for the data from the unauthorized

methods.

Note: Note: In C++ language data abstraction can be achieve by using class.

Encapsulation in C++

Encapsulation is a process of wrapping of data and methods in a single unit. It is achieved in C+

+ language by class concept.

Combining of state and behavior in a single container is known as encapsulation. In C++

language encapsulation can be achieve using class keyword, state represents declaration of

variables on attributes and behavior represents operations in terms of method.

Advantage of Encapsulation

The main advantage of using of encapsulation is to secure the data from other methods, when

we make a data private then these data only use within the class, but these data not accessible

outside the class.

Real Life Example of Encapsulation in C++

The common example of encapsulation is Capsule. In capsule all medicine are encapsulated in

side capsule.
Benefits of encapsulation

 Provides abstraction between an object and its clients.

 Protects an object from unwanted access by clients.

 Example: A bank application forbids a client to change an Account's balance.


Example of Encapsulation in C++

#include<iostream.h>
#include<conio.h>

class sum
{
private: int a,b,c;

public:
void add()
{
clrscr();
cout<<"Enter any two numbers: ";
cin>>a>>b;
c=a+b;
cout<<"Sum: "<<c;
}
};
void main()
{
sum s;
s.add();
getch();
}

Output

Enter any two number:

Sum: 9

In above example all data and function are bind inside class sum.

Polymorphism in C++

The process of representing one Form in multiple forms is known as Polymorphism. Here one

form represent original form or original method always resides in base class and multiple forms

represents overridden method which resides in derived classes.

Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many

and morphs means forms. So polymorphism means many forms.

Real life example of Polymorphism in C++

Suppose if you are in class room that time you behave like a student, when you are in market at

that time you behave like a customer, when you at your home at that time you behave like a son

or daughter, Here one person have different-different behaviors.


Type of polymorphism

 Compile time polymorphism

 Run time polymorphism

Compile time polymorphism

In C++ programming you can achieve compile time polymorphism in two way, which is given

below;

 Method overloading

 Method overriding

Method Overloading in C++

Whenever same method name is exiting multiple times in the same class with different number of

parameter or different order of parameters or different types of parameters is known as method

overloading. In below example method "sum()" is present in Addition class with same name but

with different signature or arguments.


Example of Method Overloading in C++
#include<iostream.h>
#include<conio.h>

class Addition
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(int a, int b, int c)
{
cout<<a+b+c;
}
};
void main()
{
clrscr();
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30);
}

Output

30

60

Method Overriding in C++

Define any method in both base class and derived class with same name, same parameters or

signature, this concept is known as method overriding. In below example same method

"show()" is present in both base and derived class with same name and signature.
Example of Method Overriding in C++
#include<iostream.h>
#include<conio.h>

class Base
{
public:
void show()
{
cout<<"Base class";
}
};
class Derived:public Base
{
public:
void show()
{
cout<<"Derived Class";
}
}

int mian()
{
Base b; //Base class object
Derived d; //Derived class object
b.show(); //Early Binding Ocuurs
d.show();
getch();
}

Output

Base class

Derived Class

Run Time Polymorphism


In C++ Run time polymorphism can be achieve by using virtual function.

Basic C++ Programs

Here we discuss following C++ Programs in simple and easy way.

 C++ Programs to Check Even and Odd Number


 C++ Programs to Swap two numbers
 C++ Programs to Check Given number is Prime or not
 C++ Programs to Find Factorial of Number
 C++ Programs to Print Table of any Number
 C++ Programs to Reverse any Number
 C++ Programs to Find Number Of Digits
 C++ Programs to Generate Fibonacci Series
 C++ Programs to Check Armstrong Number
 C++ Programs to Find Greatest Number
 C++ Programs to Check Given Number is Palindrome or not
 C++ Programs to Check Palindrome String
 C++ Programs to HCF or GCD of Two Numbers
 C++ Programs to Find LCM of Two Numbers
 C++ Programs to Find Square Root of Number
 C++ Programs to Find Cube Root of Number
 C++ Programs to Find Ascii Value of Number
 C++ Programs to Calculate Sum Of Digits
 C++ Programs to Find Power of a Number
 C++ Programs to Write BuzzFizz Program
 C++ Programs to Conversion of Numbers

Array Based Basic C++ Programs

 C++ Programs to Sort Array Element


 C++ Programs to Addition of Two Matrix
 C++ Programs to Pass Array In Function
 C++ Programs to Merge Any Array
 C++ Programs to Insert Element in Array
 C++ Programs to Delete Array Element
 C++ Programs to Even-Odd Array Element
 C++ Programs to Reverse Array Elements
 C++ Programs to Reverse Array Element Using Function
 C++ Programs to Sum of Array Elements
 C++ Programs to Find Duplicate Array Element

String Based Basic C++ Programs

 C++ Programs to Find length of String


 C++ Programs to Compare Two Strings
 C++ Programs to Reverse of String
 C++ Programs to Count Freq. of Char
 C++ Programs to Combined Two String
 C++ Programs to Copy String
 C++ Programs to Count Vowels in String

You might also like