You are on page 1of 310

Learn C++ Online

LEARN C++ ONLINE

Interested in learning C Programming? Click here...

FUNDAMENTALS OF C PLUS PLUS (C++) 5 JUL, 2015

Introduction to C++ Programming


As the name suggest, C++ is a programming language based on C with some added features (represented by ++) which makes possible the creation
of comparatively efficient programs. C++ programming inherits all the good features of C by filtering out some of the limitations of C. In addition,
C++ programming also possesses some extremely new unique concepts which were not at all present in C. However, since C++ is based on C, it is
extremely important to have a thorough knowledge of C. However, by chance you are not thorough with C Programming then you are strictly
recommended to have...

CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++ 2 JUL, 2015

Introduction to Inheritance in C++


Let me introduce you to the concept of Inheritance in C++. Reusability is yet another important feature of C++. It is always nice if we could reuse
something that already exists rather than trying to create the same all over again. It would not only save the time and money but also reduce
frustration and increase readability. For instance, the reuse of a class that has already been tested, debugged and used many time can save us the
effort of developing and testing the same again. Fortunately, C++ strongly supports the concept of reusability.The C++ classes can be reused in...

OPERATORS IN C++ 26 APR, 2015

sizeof operator in C++


sizeof operator in C++ is an unary operator which can be used to find the size of the data-type or expression or a variable. It returns the size of the
memory allocated (in Bytes) by the compiler. Lets say, we want to find the size of the memory allocated to the data-type int. We can find it by
using sizeof operator as shown in the below code:
C++
1
2
3
4
5
6
7
8
9
10

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sizeOfInt;
sizeOfInt = sizeof(int);
cout << sizeOfInt << " Bytes";
getch();
}

Output:
C++
1 2 Bytes

As you can see, the above code calculated the size of int data type and displayed the result in C++.

PROGRAMS IN C++ 12 APR, 2015

C++ program using two constructor to calculate minutes and seconds with hour as input from user
Write a class with name Time. Make a constructor which calculates the given time in minutes {if input time is 1hr, then the constructor should
return 60minutes. }. Make another constructor which calculates the time in seconds. { if the input time is 1hr, then the constructor should return
60*60=3600 seconds}. Display the contents of both the constructors
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#include<iostream.h>
#include<conio.h>
class Time
{
public:
Time(int);
Time(float);
};
Time::Time(int b)
{
cout << "Time in seconds: " << b*60*60 << "n";
}
Time::Time(float b)
{
cout << "Time in Minutes: " << int(b*60) << "n";
}
void main()
{
clrscr();
int hr;
cout << "Enter the time in Hours: ";
cin >> hr;
Time t1(hr);
Time t2(float(hr));
getch();
}

Output
C++
1 Enter the time in Hours: 1
2
3 Time in seconds: 3600
4 Time in Minutes: 60

PROGRAMS IN C++ 12 APR, 2015

Program in C++ to find weekday based on weekday number


The below program in C++ will find out weekday based on the weekday number input. We will be following the below logic. 1 -> Sunday 2 ->
Monday 3 -> Tuesday 4 -> Wednesday 5 -> Thursday 6 -> Friday 7 -> Saturday Here we are accepting the week day number as input from the
user and use switch statement to display the weekday. Below is the program in C++.
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include<iostream.h>
#include
#include
void main(){
int day;
clrscr();
cout << "Enter the day number: "; cin >> day;
switch(day){
case 1:
cout << "nIts Sunday";
break;
case 2:
cout << "nIts Monday";
break;
case 3:
cout << "nIts Tuesday";
break;
case 4:
cout << "nIts Wednesday";
break;
case 5:
cout << "nIts Thursday";
break;
case 6:
cout << "nIts Friday";
break;
case 7:
cout << "nIts Saturday";
break;
default:
cout << "nPlease enter number between 1 to 7";
break;
}
getch();
}

Output
C
1 Enter the day number: 5
2
3 Its Thursday

Next Page

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program

Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

FUNDAMENTALS OF C PLUS PLUS (C++)

Introduction to C++ Programming


BY LEARNCPPONLINE JULY 5, 2015

As the name suggest, C++ is a programming language based on C with some added features (represented by ++ ) which makes possible the creation of
comparatively efficient programs. C++ programming inherits all the good features of C by filtering out some of the limitations of C. In addition, C++
programming also possesses some extremely new unique concepts which were not at all present in C.
However, since C++ is based on C, it is extremely important to have a thorough knowledge of C. However, by chance you are not thorough with C Programming
then you are strictly recommended to have a look at Learn C Online tutorial www.LearnCOnline.com. Remember, it is extremely important to have a strong
foundation in order to build a strong structure.

Now, let us turn our attention to the study of C++. So, lets start our journey towards the study of C++

Tags:

Best C++ Online Tutorial

C Plus Plus

C++

Introduction to C++

Learn C++ Online

YOU MAY ALSO LIKE...


0

Variables and Declarations in C++

Logical Operators in C++

11 AUG, 2013

11 AUG, 2013

2 RESPONSES

Comments 2

Pingbacks 0

Daniel November 13, 2013 at 10:27 pm


Nice one
Reply
Alex December 21, 2013 at 12:21 am
This is quite an interesting site to learn c++ online for the beginners .it gives knowledge of every topic which is useful in understanding C++ better
Reply

LEAVE A REPLY
Name *

Email *

Website

+ 5 = eight
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions

Introduction to Inheritance in C++

Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

FUNDAMENTALS OF C PLUS PLUS (C++)

Comment Statements in C++


BY LEARNCPPONLINE AUGUST 10, 2013

Unlike C, C++ supports two types of comments. C programmers are familiar with the /*..*/ style of commenting. Anything lying within the
ignored by the compiler. C plus plus (C++) additionally supports the // notation of commenting.

Here, everything following // till the end of line is treated as comment.


Usually /*..*/ style is used for commenting out of a block code, whereas,

//

/*..*/

pair is

is used for single-line comments.

Example of single-line comment:


C++
1 // This is a single line comment

Example of multiple-line comment:


C++
1 /*This is
2 a multiple line comment*/

Tags:

Best C++ Online Tutorial

comments in C++

Learn C++ Online

multiple lines comment in C++

single line comment in C++

YOU MAY ALSO LIKE...


1

Identifiers and Keywords in C++

Operators and Expressions in C++

11 AUG, 2013

11 AUG, 2013

LEAVE A REPLY
Name *

Email *

Website

3 = three
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Identifiers and Keywords in C++

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions

Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

FUNDAMENTALS OF C PLUS PLUS (C++)

Identifiers and Keywords in C++


BY LEARNCPPONLINE AUGUST 11, 2013

In a C++ program (i.e., a program written in C++ language), every word is either classified as an identifier or a keyword.
Identifiers, as the name suggests, are used to identify or name various program elements such as variables, constants, functions etc.
On the other hand, Keywords are a kind of reserved word which have standard, predefined meanings in C++.
Now, let us discuss these concepts in details:

Identifiers:
As stated before, identifiers are names that are given to various program elements such as variables, constants, functions, arrays etc. There are certain rules
regarding identifier names in C++, as stated below:
1. Identifiers must consist of letters and digits, in any order, except that the first character must be a letter
2. Both upper-case and lower-case letters are permitted, though common usage favours the use of lower-case letters for most types of identifiers. Uppercase and lower-case letters are not interchangeable i.e., an upper-case letter is not equivalent to the corresponding lower-case letter. Thus the names
rate, Rate and RATE denote different identifiers. It is a general practice to use lower-case or mixed case for variable and function names and upper-case
for constants
3. The underscore character (_) can also be included, and is considered to be a letter. An underscore is often used in the middle of an identifier. An identifier
may also begin with an underscore, though this is rarely done in practice
4. An identifier should contain enough characters so that its meaning is readily apparent. On the other hand, an excessive number of characters should be
avoided
5. There are certain reserved words, called keywords, that have standard, predefined meanings in C++. These keywords can be used only for their intended
purpose. They cannot be used as a programmer-defined identifiers.
6. It is required to note here that the keywords are all lower-case. Since upper-case and lower-case characters are not equivalent, it is possible to utilize an
upper-case keyword as an identifier. Normally, however, this is not done, as it is considered a poor programming practice
Examples of valid identifiers:
z
x11
sum_1
_temperature
names
area
Examples of Invalid identifiers:

7th
a
order-no
error flag
Keywords:
As stated before, keywords are the words whose meaning has already been explained to the C++ compiler (or in a broad sense to the computer). The
keywords cannot be used as identifier names because, if we do so, we are trying to assign a new meaning to the keyword, which is not allowed by the
computer. The keywords are also called Reserved words.
Examples of Keywords in C++:
and
or
register
char
int
float
class
function
public
private
private
switch

Tags:

Identifiers in C plus plus (C++)

Keywords in C plus plus (C++)

rules for identifiers

YOU MAY ALSO LIKE...


1

Arithmetic Operators in C++

Comment Statements in C++

11 AUG, 2013

10 AUG, 2013

1 RESPONSE

Comments 1

Pingbacks 0

Anish January 22, 2015 at 8:31 pm


Sir,
My practical exams are going to start in a few days.I have some problem in C++ programs. I am unable to remember the program and tend to forget it in a few days.
Can you please suggest me on how to perfectly remember the programs as we also have to explain the program after execution.. Pls help..
Reply

LEAVE A REPLY

Name *

Email *

Website

seven +

= 11

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Data Types in C++

PREVIOUS STORY

Comment Statements in C++

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

FUNDAMENTALS OF C PLUS PLUS (C++)

Data Types in C++


BY LEARNCPPONLINE AUGUST 11, 2013

Data types supported by C++ can be broadly divided into six heads as stated follows:
1.
2.
3.
4.
5.
6.

data-type
char data-type
float data-type
double data-type
bool (boolean) data-type
enum (enumeration) data-type
int

1) int data-type:
int data-type represents the whole number (i.e., integer) quantities. Integers are required not to contain a decimal point or an exponent.
int

data-type can also be further sub-divided into two broad categories, viz,

1. short int and long int


2. signed int and unsigned int
short int and long int
The qualifier short, when placed in front of the int declaration, tells the C++ system that the particular variable being declared will be used to store fairly small
integer values.
On the other hand, the qualifier long, when placed in front of the int declaration, tells the C++ system that the particular variable being declared will be used to
store fairly large integer values.
signed int and unsigned int
In case of signed int (or even in case of ordinary int, short int, long int as by default every int is signed int), the leftmost bit (of computer memory) is reserved
for the sign. However in case of unsigned int, there is no such kind of reservation and so all the bits are used to represent the numeric value. Thus, an unsigned
int can be approximately twice as large as an ordinary int.
2) char data-type:
char data-type is used to represent individual characters. Hence, the char type generally requires only one byte of memory. Just like signed int and unsigned
int, C++ language permits signed char and unsigned char.

3) float data-type:
float data-type (also called as floating point) represents values containing decimal places. A floating point value is distinguished by the presence of a decimal
point. It is permissible to omit digits before the decimal point, or digits after the decimal point, but obviously not permissible to omit both.
The values 3., 125.8 and -.001 are all valid examples of floating point values.

Floating point values can also be expressed in so called scientific notation. The value 17.e4 is a floating point value expressed in this notation, and represents
the value 1.7 x 104. The value before the letter e is known as the mantissa while the value that follows the letter e is called the exponent.
4) double data-type:
double data-type is very similar to float data-type. It is used whenever the accuracy provided by a float variable is not sufficient. Variables declared to be of
type double can store roughly twice as many significant digits as can a variable of type float can store.
5) bool (boolean) data-type:
This is a new addition to C++. The data type bool gets its name from George Boole, a 19th century English mathematician who invented the concept of using
logical operators with true or false values. These values are often called boolean values.
This data type can take only two values true or false. It is commonly used to hold the results of comparisons.
For example:
C++
1
2
3
4

bool a,
int x =
a = x <
b = y>=

Here,

b;
10, y = 20, z = 30;
y;
z;

gets a value

By definition,

true

true . whereas, b

gets a value

false .

has a value 1 (when converted to an integer) and

false

has a value 0 (when converted to an integer).

Programming example along with its output:


C++
1
2
3
4
5
6
7
8

int main()
{
bool flag = false;
cout << "flag = " << flag <<endl;
flag = true;
cout << "flag = " << flag << endl;
}

Output:
flag = 0
flag = 1

6) enum (enumeration) data-type:


An enumeration data type is an integral type that is defined by the user and has the syntax as:
enum typename {enumerator-list};
where enum is a C++ keyword,
typename stands for an identifier that names the type being defined, and
enumerator-list stands for a list of names for integer constants.
For example, the following defines the enumeration type semester, specifying the three possible values that a variable of that type can have:
C++
1 enum semester {FALL, SPRING, SUMMER};

We can then declare the variables of this above type as:


C++
1 semester s1, s2;

and we can use those variables and those type values as we would with predefined types as:
C++
1
2
3
4
5
6

s1 = SPRING;
s2 = FALL;
if (s1 == s2)
{
cout << "Same Semester." <<endl;
}

Tags:

Best C++ Online Tutorial

bool data type. enum data type

char data type. float data type

data types in C plus plus (C++)

double data type

int data type

Learn C++ Online

YOU MAY ALSO LIKE...


0

Input and Output in C++

Relational Operators in C++

17 AUG, 2013

11 AUG, 2013

3 RESPONSES

Comments 3

Pingbacks 0

Eric September 18, 2014 at 7:27 am


I blog quite often and I truly thank you for your information. Your article has really peaked my interest.
Im going to take a note of your blog and keep checking for new
information about once a week. I subscribed to your Feed
as well.
Reply
Jake August 21, 2015 at 1:16 am
In #4 above (describing the double data type), theres a typo that can cause confusion to the reader: Variables declared to be of type float can store roughly twice
as many significant digits as can a variable of type float can store.
It should read: Variables declared to be of type __double__ can store roughly twice as many significant digits as can a variable of type float can store.
Reply

Prashant N. August 23, 2015 at 2:35 pm


Thank a lot Jake for pointing it out. I have made the necessary correction.
Reply

LEAVE A REPLY
Name *

Email *

Website

two +

=6

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Variables and Declarations in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement

Identifiers and Keywords in C++

do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

FUNDAMENTALS OF C PLUS PLUS (C++)

Variables and Declarations in C++


BY LEARNCPPONLINE AUGUST 11, 2013

Variables in C Plus Plus (C++):


A variable can be defined as a quantity that varies during program execution.
A variable is a symbol that represents a storage location in the computers memory. The information that is stored in that location is called the value of the
variable. One common way for a variable to obtain a value is by an assignment. This has the syntax:
variable = expression

First, the

expression

is evaluated and then the resulting value is assigned to the

variable . The equal sign = is the assignment operator in C++.

Declarations in C++:
A declaration associates a group of variables with a specific data-type. All variables must be declared before they can appear in executable statements.

A declaration consists of a data-type, followed by one or more variable names, ending with a semicolon.
Example:
C++
1 int a, b, c;
2 float root1, root2;
3 char flag;

Here, a,

and

are declared to be integer variables,

root1

and

root2

are floating-point variables and

flag

is a char-type variables.

These declarations could also have been written as follows:


C++
1
2
3
4
5
6

int a;
int b;
int c;
float rot1;
float root2;
char flag;

Initial values can be assigned to variables within a type-declaration. To do so, the declaration must consist of a data-type, followed by a variable name, an equal
sign (=) and a constant of the appropriate type. A semicolon must appear at the end, as usual.
Example:
C++
1 int c = 12;

2 char star = '*';


3 float sum = 0.;
4 double factor = 0.123458e-6;

Thus, in above example,

is an integer variable whose initial value is 12,

variable whose initial value is 0. and

Tags:

declarations in C++

factor

star

ia a char-type variable initially assigned the character *,

sum

is a floating-point

-6

is a double-precision variable whose initial value is 0.123458 x 10 .

variables and declarations in C plus plus

variables in C++

YOU MAY ALSO LIKE...


0

Conditional Operator in C++

Introduction to C++ Programming

11 AUG, 2013

5 JUL, 2015

LEAVE A REPLY
Name *

Email *

Website

7+

= nine

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Operators and Expressions in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Data Types in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

EXPRESSIONS IN C++ / FUNDAMENTALS OF C PLUS PLUS (C++) / OPERATORS IN C++

Operators and Expressions in C++


BY LEARNCPPONLINE AUGUST 11, 2013

An operator, in general, is a symbol that operates on a certain data-type. For example, the operator + is the addition operator. It can operate on integer,
character and real (float and double) numbers.

On the other hand, an expression is a combination of variables, constants and operators written according to the syntax of the language.
Types of Operators:
1.
2.
3.
4.
5.
6.
7.

Tags:

Arithmetic operators
Unary operators
Increment and Decrement operators
Relational operators
Logical operators
Assignment operators
Conditional operators

Arithmetic operators

Relational operators

Assignment operators

Conditional operators

expressions in C++

Increment and Decrement operators

Logical operators

operators in C++

Unary operators

YOU MAY ALSO LIKE...


1

Preprocessor Directives in C++

Identifiers and Keywords in C++

17 AUG, 2013

11 AUG, 2013

LEAVE A REPLY
Name *

Email *

Website

+ 4 = seven
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Arithmetic Operators in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance

Variables and Declarations in C++

Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

FUNDAMENTALS OF C PLUS PLUS (C++) / TYPE CONVERSION IN C++

Type Conversion in C++


BY LEARNCPPONLINE AUGUST 11, 2013

Type Conversion means conversion of one data-type to another data-type. In C Plus Plus (C++), type conversion can be done by two ways:
1. Automatic Conversion
2. Type Casting
1) Automatic Conversion:
In C++, if we have mixed mode expression (i.e., different types of data in one expression) then the lower data-type variable is automatically converted to the
data-type of the higher data-type variable.

Let us illustrate this with am example as follows:


C++
1 int c = 7;
2 float a = 155.5;
3 double t = c * a;

In the above example, in expression:


C++
1 double t = c * a;

firstly, variable c of the type


the result of multiplication c

int
* a

is converted to type float and is stored in a temporary variable before being multiplied by variable a of type float . Also,
which comes in type float is then converted to type double and then is assigned to variable t of type double .

2) Type Casting
The casting is a technique by which forcefully we can convert one data-type to other data-type. The operator used for this purpose is known as cast operator.
The cast operator takes on any of the following format:
(cast-type) expression;

or
cast-type (expression);

Where cast-type refers to the data-type to which we desire to convert the


variable.

expression

to, and

expression

is any valid C++ expression or it may be a single

Example of Type Casting:


C++
1 int a, b;
2 float c;
3 a = 200;

3 a = 200;
4 b = 400;
5 c = (float) a * b;

In the above example, variable


expression ( (float) a ).

Tags:

automatic type conversion

is converted into

type casting

float

data-type before getting multiplied by variable

because of the use of the cast operator in the

type conversion in C++

YOU MAY ALSO LIKE...


0

Arrays in C++

Operators and Expressions in C++

12 OCT, 2013

11 AUG, 2013

LEAVE A REPLY
Name *

Email *

Website

9 three =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Input and Output in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Conditional Operator in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

FUNDAMENTALS OF C PLUS PLUS (C++) / INPUT AND OUTPUT IN C++

Input and Output in C++


BY LEARNCPPONLINE AUGUST 17, 2013

Output in C++:
Output in C Plus Plus (C++) is enabled by the object cout (pronounced as C out) which is predefined to corresponding to the standard output stream. A
stream is an abstraction that refers to a flow of data. The standard output stream normally flows to the screen display, although it can be redirected to other
output devices.
cout , when combined with insertion or put-to operator <<

enables the output in C++ programs.

<<

operator redirects the contents of the variable on its (<<s)

right to the object on its(<<s) left.


A simple example of output of a phrase is depicted below:
C++
1 cout << "LearnCppOnline.com is the best C++ programming tutorial site";

Above statement causes the phrase in the quotation marks to be displayed on the screen.
Input in C++
Contrary to cout , to receive input through the keyboard what is used is object cin (pronounced as C in). cin is a object predefined in C++ to correspond to
the standard input stream. This stream represents the data coming from the keyboard (unless it has been redirected).
cin , when combined with extraction or get-from operator >>

enables the input in C++ programs.

>>

operators takes the value from the object on its ( >> s)

left and places it in the variables on its right.


A simple example of input of the value of a single variable is depicted below:
C++
1 cin >> n;

Above statement causes the value provided from the keyboard to get assigned to the variable n.

Cascading of Input and Output in C++


Cascading of input and output is also possible in C++, Let us illustrate this with the following examples:
Example depicting cascading of Input
Suppose we want to read the values from the variables n1, n2 and n3. We can do this with the single statement:
C++
1 cin >> n1 >> n2 >> n3;

Above one statement is equivalent to the following three statements:

C++
1 cin >> n1;
2 cin >> n2;
3 cin >> n3;

Example depicting cascading of Output


Suppose we want to print the values of the variables n1 and n2. We can do this with the single statement:
C++
1 cout << "n1 = " << n1 << "n2 = " << n2;

Above one statement is equivalent to the following four statements:


C++
1
2
3
4

cout
cout
cout
cout

<<
<<
<<
<<

"n1 = ";
n1;
"n2 = ";
n2;

A Special Mention of Header File required for Input and Output in C++
To enable the use of cout , << , cin and >> , one needs to include a header file named

iostream.h

in the first line of every program, by the statement:


C++

1 #include <iostream.h>

This header file contains the declaration that are needed by cout & cin objects and << &
recognize cout & cin and will think << & >> are being used incorrectly in the program.

Tags:

cin

cout

Input in C++

>>

operators. Without this declaration, the compiler wont

Output in C++

YOU MAY ALSO LIKE...


2

Introduction to C++ Programming

Variables and Declarations in C++

5 JUL, 2015

11 AUG, 2013

LEAVE A REPLY
Name *

Email *

Website

four +
Comment

= 13

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Preprocessor Directives in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements

Type Conversion in C++

if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

FUNDAMENTALS OF C PLUS PLUS (C++) / PREPROCESSOR DIRECTIVES IN C++

Preprocessor Directives in C++


BY LEARNCPPONLINE AUGUST 17, 2013

The C++ preprocessor is a collection of special statements, called directives, that are executed at the begenning of the compilation process.
The preprocessor is usually an integral part of our compiler. It is a process that is executed by the compiler before our C++ program code is compiled into
machine instructions. The job of the preprocessor is to prepare our source code, proper for the compile phase, according to the instructions that we have
included in the source files. These instructions are called preprocessor directives.

All preprocessor directives begin with the symbol #, so they are easily distinguishable from C++ language statements.
Given below is the complete set of preprocessor directives:
Directive

Function

#include

Supports header file inclusions

#if, #else, #elif, #endif, #if defined (or #ifdef), #if !defined (or #ifndef)

Enables conditional compilation

#define, #undef

Enable symbol and macro substitution

#line

Allows redefinition of current line and filename

#error

Produces compile-time error message

#paragma

Offers machine-specific features while retaining overall C++ compatibility

We have already used preprocessor directives in out past examples,and we are very familiar with the

Tags:

#define

#elif

#else

#endif

#if

#inlcude

#line

#rror

#include

directive by now.

Preprocessor Directives in C++

YOU MAY ALSO LIKE...


0

Structures in C++

Data Types in C++

13 OCT, 2013

11 AUG, 2013

1 RESPONSE

Comments 1

Pingbacks 0

Derrick September 20, 2014 at 5:24 am


Its very effortless to find out any topic on net as compared
to textbooks, as I found this piece of writing at this web site.
Reply

LEAVE A REPLY
Name *

Email *

Website

6 = eighteen
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Control Statements in C Plus Plus (C++)

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Input and Output in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONTROL STATEMENTS IN C++

Control Statements in C Plus Plus (C++)


BY LEARNCPPONLINE SEPTEMBER 15, 2013

Control statements alter the flow of execution of the programs. Control statements can be broadly divided into three categories:
1. Decision-making or Conditional Statements (Branching and Selection)
1. if statement
2. if-else statement
3. switch statement
2. Loop Statements
1. for statement
2. while statement
3. do-while statement
3. Breaking Control Statements
1. break statement
2. continue statement
3. goto statement

Tags:

#if

break

continue

control statements

do-while

for

goto

if-else

switch

while

YOU MAY ALSO LIKE...


5

while statement in C++

for statement in C++

22 SEP, 2013

22 SEP, 2013

LEAVE A REPLY
Name *

Email *

Website

six

=4

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

If Statement in C++

PREVIOUS STORY

Introduction to C++

Preprocessor Directives in C++

Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.


About Us | Copyright Notice | Privacy Policy | Website Disclaimer
LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

BRANCHING AND SELECTION IN C++ / CONDITIONAL STATEMENTS IN C PLUS PLUS / CONTROL STATEMENTS IN C++ / DECISION-MAKING STATEMENT IN C++

If Statement in C++
BY LEARNCPPONLINE SEPTEMBER 15, 2013

if statement:
The if statement is used to express the conditional expressions. If the given condition is true then it will execute the statements otherwise it will execute the
optional statements.

The basic simple structure of the

if

statement is shown below:


C++

1
2
3
4

if (expression)
{
//set of statements
}

The expression must be placed in round brackets as shown above. In this form, the set of statements would be executed only if the expression has a non-zero
value (i.e., if expression is true). If the expression has a value zero (i.e., if expression is false), then the set of statements would be ignored by C++ compiler. The
set of statements are skipped and the execution continues with the next statements.
Example:
Given below is a program which reads two numbers and then prints greater one using if statement.
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Tags:

#include <iostream.h>
void main()
{
int a, b;
cout << "Enter Two Numbers:";
cin >> a >> b;
if (a > b)
{
cout << "Greater is " " << a;
}
if (b > a)
{
cout << "Greater is " " << b;
}
}

#if

if statement

YOU MAY ALSO LIKE...


5

while statement in C++

If-else Statement in C++

22 SEP, 2013

15 SEP, 2013

1 RESPONSE

Comments 0

Pingbacks 1

LEAVE A REPLY
Name *

Email *

Website

6 five =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

If-else Statement in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Control Statements in C Plus Plus (C++)

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

BRANCHING AND SELECTION IN C++ / CONDITIONAL STATEMENTS IN C PLUS PLUS / CONTROL STATEMENTS IN C++ / DECISION-MAKING STATEMENT IN C++

If-else Statement in C++


BY LEARNCPPONLINE SEPTEMBER 15, 2013

if-else statement
if statement is most commonly used with the following format:
C++
1
2
3
4
5
6
7
8

if(expression)
{
statement 1
}
else
{
statement 2
}

where

else

part is optional.

In this case, either of the two statements are executed depending upon the value of the expression. If the expression has a non-zero value (i.e., if the
expression is true), then statement1 will be executed. Otherwise (i.e., if expression is false), statement2 will be executed.
Example:
Given here is a program which reads a number and then prints whether the given number is even or odd.
Note: If we divide a number by 2 and if the remainder is 0 then the number is EVEN else the number is ODD
C++
1
2
3
4
5
6
7
8
9
10
11
12

Tags:

#include<iostream.h>
void main()
{
int n;
cout << "Enter a number :";
cin >> n;
if(n%2 == 0)
cout << "The number is EVEN";
else
cout << "The number is ODD";
}

if-else

if-else statement

YOU MAY ALSO LIKE...


0

Control Statements in C Plus Plus (C++)

for statement in C++

15 SEP, 2013

22 SEP, 2013

1 RESPONSE

Comments 0

Pingbacks 1

LEAVE A REPLY
Name *

Email *

Website

nine = 81
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Switch Statement in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

If Statement in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

BRANCHING AND SELECTION IN C++ / CONDITIONAL STATEMENTS IN C PLUS PLUS / CONTROL STATEMENTS IN C++ / DECISION-MAKING STATEMENT IN C++

Switch Statement in C++


BY LEARNCPPONLINE SEPTEMBER 16, 2013

The

statement is a special multiway decision maker that tests whether an expression matches one of the number of constant values accordingly.
switch statement allows the user to choose a statement or a group of statements among several alternatives. The switch statement is useful when a
variable is compared with different constants, and in case it is equal to a constant, a set of statements would be executed.
switch

The general form of the switch statement is as follows:


C++
1
2
3
4
5
6
7
8
9
10
11
12

switch (expression) {
case constant 1:
statement;
case constant 2:
statement;
..........
..........
case constant n:
statement;
default:
statement;
}

In this general form:


1) The expression, whose value is being compared, may be any valid expression but not a floating point expression
2) The The value that follows the keyword case may only be constants. They cannot be expressions. They may be an integer or characters, but not the floating
point numbers or character string
3) The constants in each of the case statements must obviously be of same type
4) The expressions value is checked against each of the specified case and when the match occurs, the statement following the case is executed. Again, to
maintain generality, the statement can be either a simple or a compound statement
5) The last case of this statement which is called the default case is optional and should be used according to the programs specific requirement. If no match
is found, then the statement in the default case would be executed. If the default case is not included in a switch statement and the expression is not
matched by any other cases, nothing happens and the statement following the switch construct is executed.
Execution of the switch in C++ follows a different logic. No statements are executed until a case has been matched or the default case has been
encountered. However, it continues to execute all statements once a case has been matched; irrespective of the fact that whether those statements belong
to the case that has been matched or not.
C++ offers a method of overcoming this side-effect of
immediate exit from the switch construct.
In general, it is advisable to use the

break

statement.

switch

statement with the help of the break statement. The

break

statement will cause an

Thus the general form of the mutually exclusive

switch

statement will be:


C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

switch (expression) {
case constant 1:
statement;
break;
case constant 2:
statement;
break;
..........
..........
case constant n:
statement;
break;
default:
statement;
}

Example:
Program which reads a number, between 1 to 7 and then prints the day corresponding to that number
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

Tags:

#include<iostream.h>
void main()
{
int n;
cout << "Enter a day number between 1 to 7 :";
cin >> n;
switch(n){
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
default:
cout << "Enter correct number";
}
}

switch

switch c++

switch statement

YOU MAY ALSO LIKE...


1

do-while statement in C++

break statement in C++

29 SEP, 2013

5 OCT, 2013

2 RESPONSES

Comments 0

Pingbacks 2

LEAVE A REPLY
Name *

Email *

Website

+ 5 = fourteen
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

for statement in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance

If-else Statement in C++

Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONTROL STATEMENTS IN C++ / LOOP STATEMENTS IN C++

for statement in C++


BY LEARNCPPONLINE SEPTEMBER 22, 2013

The

for

statement or

The

for

loop is the most commonly used statement in C++. This loop consists of three expression:

for

loop is useful while executing a statement multiple number of times.

The first expression is used to initialize the index value


The second expression is used to check whether or not the loop is to be continued again
The third expression is used to change the index value for further iteration
The general form of

for

statement is:
C++

1 for(expression 1; expression 2; expression 3)


2 statement;

In other words,
C++
1
2
3
4
5
6
7

for(initial condition; test condition; incrementer or decrementer)


{
statement1;
statement2;
.....
.....
}

Where,

expression 1 is the initialization of the expression or the condition called an index


expression 2 is the condition checked. As long as the given expression is true the loop statement will be repeated
expression 3 is the incrementer or decrementer to change the index value of the for loop variable
Typically, expression 1 is an assignment expression, expression 2 is a logical expression and expression 3 is a unary expression or an assignment expression.
When the for statement is executed, expression 2 is evaluated and tested at the begening of each pass through the loop and expression 3 is evaluated at the
end of each pass.
Program to compute a sum of consecutive integers, i.e., the program to compute the sum 1 + 2 + 3 + .. + n, for an integer input n.
C++
1
2
3
4

#include
{
int n;
cout << "Enter a positive integer: "; cin >> n;

5
6
7
8
9
10
11
12
13

Tags:

long sum = 0;
for(int i=1; i<=n; i++)
{
sum = sum + i;
}
cout << "Sum of first " << n << " integers is " << sum;
}

for loop statement

for statement.c plus plus for statement

YOU MAY ALSO LIKE...


5

while statement in C++

do-while statement in C++

22 SEP, 2013

29 SEP, 2013

3 RESPONSES

Comments 2

Pingbacks 1

Daniel November 14, 2013 at 11:40 pm


What will be the output of that above program?
Reply

LearnCPPOnline November 23, 2013 at 1:16 am


Output:
Enter a positive integer: 3
Sum of first 3 integers is 6
Reply

LEAVE A REPLY
Name *

Website

six + 9 =
Comment

Email *

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

while statement in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement

Switch Statement in C++

if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONTROL STATEMENTS IN C++ / LOOP STATEMENTS IN C++

while statement in C++


BY LEARNCPPONLINE SEPTEMBER 22, 2013

The second type of loop, the while loop, is used when we are not certain that the loop will be executed. After checking whether the initial condition is true or
false and finding it to be true, then only while loop will enter into the loop operations.
The general form of the

while

loop for a single statement is:


C++

1 while(expression)
2 statement;

The general form of the

while

loop for a block of statements is:


C++

1
2
3
4
5
6
7

while(expression)
{
statement 1;
statement 2;
....
....
}

The expression can be any valid C++ language expression including the value of a variable, an unary or a binary expression, or the value returned by a function.
The statement can be single or compount statement.

The statement will be executed repeatedly, as long as the expression is true (i.e., as long as expression has a non zero value). statement must include some
features that eventually alters the value of the expression, thus providing a stopping condition for the loop.
Program to compute a sum of consecutive integers, i.e., the program to compute the sum 1 + 2 + 3 + .. + n, for an integer input n
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#include
int main()
{
int n, i=1;
cout << "Enter a positive integer: "; cin >> n;
long sum = 0;
while(i<=n)
{
sum = sum + 1;
i = i + 1;
}
cout << "Sum of first " << n << " integers is: " << sum;
}

Tags:

c plus plus while statement

while loop statement

while statement

while statement in c plus plus

YOU MAY ALSO LIKE...


3

goto statement in C++

Switch Statement in C++

5 OCT, 2013

16 SEP, 2013

5 RESPONSES

Comments 4

Pingbacks 1

Frank Perrett September 2, 2014 at 5:40 pm


Im not that much of a internet reader to be honest but your sites really nice, keep it up!
Ill go ahead and bookmark your website to come back in the future.
All the best
Reply
Surya November 25, 2014 at 12:32 am
the final program will run forever. You arent incrementing variable i inside the while loop so i will never be <= 'n'.
Reply
Surya November 25, 2014 at 12:33 am
* i will never be > n
Reply

Prashant N. December 14, 2014 at 9:57 am


Thanks a lot for pointing it. We have rectified it.
Thanks again,
LearnCPPOnline Team
Reply

LEAVE A REPLY
Name *

Email *

Website

2+

= six

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

do-while statement in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion

for statement in C++

Input and Output


Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONTROL STATEMENTS IN C++ / LOOP STATEMENTS IN C++

do-while statement in C++


BY LEARNCPPONLINE SEPTEMBER 29, 2013

The

do-while

loop is another repetitive loop used in C++ programs.

When a loop is constructed using the while statement, the test for continuation of the loop is carried out at the beginning of each pass. Sometimes, however,
it is desirable to have a loop with the test for continuation at the end of each pass. This can be accomplished by means of do-while statement.
The general form of the

do-while

statement is
C++

1
2
3
4
5
6
7

do{
statement 1;
statement 2;
....
....
}
while(expression);

The statement will be executed repeatedly, as long as the value of expression is true (i.e., is non-zero). Notice that statement will always be executed at least
once, since the test for repetition does not occur until the end of the first pass through the loop. The statement can be either simple or compound. It must
include some feature that eventually alters the value of expression so that the looping action can terminate.
For many application it is more natural to test for continuation of the loop at the beginning rather than at the end of the loop. For this reason, the
statement is used less frequently than the while statement.

do-while

Program to compute a sum of consecutive integers, i.e., the program to compute the sum 1 + 2 + 3 + .. + n, for an integer input n
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include
void main()
{
int n, i = 0;
cout << "Enter a positive integer: "; cin >> n;
sum = 0;
do{
sum += i++;
}
while(i<=n);
cout << "Sum of first " << n << " integer is " << sum;
}

Tags:

do while in C plus plus

do-while

do-while loop statement

do-while statement

YOU MAY ALSO LIKE...


1

If-else Statement in C++

while statement in C++

15 SEP, 2013

22 SEP, 2013

1 RESPONSE

Comments 0

Pingbacks 1

LEAVE A REPLY
Name *

Email *

Website

3 + three =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

break statement in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

while statement in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

BREAKING CONTROL STATEMENTS IN C++ / CONTROL STATEMENTS IN C++

break statement in C++


BY LEARNCPPONLINE OCTOBER 5, 2013

The

break

statement is used to terminate loops or to exit from a

switch . It can be used within a for , a >code>while, a do-while

or a

switch

statement.

The break statement is written simply as:


C++
1 break;

without any embedded expressions or statements.


The

break

is a keyword in the C++ program and the semicolon must be inserted after the

break

statement.

We have already seen the use of break statement within the example of switch statement (Click here). The
out of the entire switch statement, to the first statement following the switch statement.

Tags:

break c plus plus

break statement

break

statement causes a transfer of control

break statement in c plus plus

YOU MAY ALSO LIKE...


1

If-else Statement in C++

while statement in C++

15 SEP, 2013

22 SEP, 2013

LEAVE A REPLY
Name *

Email *

Website

9+

= fourteen

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

continue statement in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers

do-while statement in C++

Accessing of the Pointer Variables


void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

BREAKING CONTROL STATEMENTS IN C++ / CONTROL STATEMENTS IN C++

continue statement in C++


BY LEARNCPPONLINE OCTOBER 5, 2013

The continue statement is used to bypass the remainder of the current pass through a loop. The loop does not terminate when a continue statement is
encountered. Rather, the remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop.
The

continue

statement can be included within a

The

continue

statement is written simply as:

for , a while

or a

do-while

statement.

C++
1 continue;

without any embedded expressions or statements.


The

Tags:

continue

is a keyword in the C++ program and the semicolon must be inserted after the

continue c++

continue statement

continue

statement.

continue statement c++

YOU MAY ALSO LIKE...


1

If-else Statement in C++

Switch Statement in C++

15 SEP, 2013

16 SEP, 2013

LEAVE A REPLY
Name *

Email *

Website

= one

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

goto statement in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers

break statement in C++

Accessing of the Pointer Variables


void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

BREAKING CONTROL STATEMENTS IN C++ / CONTROL STATEMENTS IN C++

goto statement in C++


BY LEARNCPPONLINE OCTOBER 5, 2013

The

goto

statement is used to alter the normal sequence of the program execution by transferring control to some other part of the program.

In its general form, the

goto

statement is written as:


C++

1 goto label;

Where label is an identifier that is used to label the target statement to which control will be transferred.
Control may be transferred to any other statement within the program. The target statement must be labeled, and the label must be followed by a colon.
Thus, the target statement will appear as:
C++
1 label: statement

Each labeled statement within the program must have a unique label; i.e., no two statements can have the same label.

Tags:

goto c++

goto statement

goto statement c plus plus

YOU MAY ALSO LIKE...


0

Control Statements in C Plus Plus (C++)

continue statement in C++

15 SEP, 2013

5 OCT, 2013

3 RESPONSES

Comments 3

Pingbacks 0

christian August 30, 2014 at 1:56 am


Hi there, just wanted to mention, I loved this post.
It was helpful. Keep on posting!
Reply
david zitting September 11, 2014 at 12:49 am
Everything is very open with a precise description of the
issues. It was truly informative. Your website is very
helpful. Many thanks for sharing!
Reply
Edwin September 22, 2014 at 1:05 pm
Hello, I enjoy reading through your post.
I wanted to write a little comment to support you.
Reply

LEAVE A REPLY
Name *

Email *

Website

two 7 =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Defining of Functions in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

continue statement in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

DEFINING OF FUNCTIONS IN C++ / FUNCTIONS IN C++

Defining of Functions in C++


BY LEARNCPPONLINE OCTOBER 6, 2013

A function definition has two principal components:


1. the first line (including the argument declaration), and
2. the body of the function
In general, the first line can be written as:
C++
1 data-type function_name(type 1 arg 1, type 2 arg 2, ..., type n arg n)

where,
data-type represents the data type of the item that is returned by the function,
function_name represents the function name, and
type 1, type 2, , type n represents the data-type of the arguments arg 1, arg 2, , arg n.
The data-types are assumed to be of the type
practice, even if the data items are integers.

int

if they are not shown explicitly. However, the omission of the data type is considered poor programming

The arguments are called formal arguments, because they represent the names of the data items that are transferred into the function from the calling
portion of the program. The names of the formal arguments need not be the same as the names of the actual arguments in the calling function of the
program. However, each formal argument must be of the same data type, as the data item it receives from the calling function of the program.
The remainder of the function definition is a compound statement that defines the action to be taken by the function. This compound statement is referred to
as the body of the function. Like any other compound statement, this statement can contain expression statements, other compound statements, control
statements, and so on.
It should include one or more

return

statements, in order to return a value to the calling function of the program.

Example of Function Definition:


C++
1
2
3
4
5
6
7

int addnumbers(int a, int b)


{
//body of the function enclosed in curly braces
int c;
c = a + b;
return c;
}

let us break the above example line by line and understand it,

C++
1 int addnumbers(int a, int b)

Here, addnumbers is the name of the function having return type as int which means the function will return a value having integer data type.
The function addnumbers has two formal arguments i.e. a and b both having data type as int i.e., integer.
C++
1
2
3
4
5
6

{
//body of the function enclosed in curly braces
int c;
c = a + b;
return c;
}

This is called the body of the function named addnumbers. The body of the function is enclosed in curly braces {}. The body of the function consist of multiple
statements.
The above function is performing addition of the values present in variables a and b, and storing the value in variable c.
C++
1 return c;

The above statement returns the result stored in the variable c to the calling function.

Tags:

define functions in C++

defining functions

defining functions in c++

function definition in c++

function in c++

YOU MAY ALSO LIKE...


0

Default Arguments in Functions

Inline Functions in C++

12 OCT, 2013

12 OCT, 2013

LEAVE A REPLY
Name *

Email *

Website

five +
Comment

=6

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Accessing of Functions in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement

goto statement in C++

for loop statement


while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

ACCESSING OF FUNCTIONS IN C++ / FUNCTIONS IN C++

Accessing of Functions in C++


BY LEARNCPPONLINE OCTOBER 6, 2013

A function can be accessed (i.e., called) by specifying its name, followed by a list of arguments enclosed in parentheses and separated by commas as shown
below.
C++
1 function_name(int a, int b);

If the function call does not require any arguments, an empty pair of parentheses must follow the name of the function as shown below.
C++
1 function_name();

The function call may be a part of a single expression (such as an assignment statement), or it may be one of the operands within the more complex
expression as shown below.
C++
1 int returnedvalue = function_name();
2 int c = 3 * function_name() + 5;

The arguments appearing in the function call are referred to as actual arguments, in contrast to the formal arguments that appear in the first line of the
function definition.
In normal function call, there will be one actual argument for each formal argument. The actual arguments may be expressed as constants, single variables or
more complex expressions. However, each actual arguments must be of the same data type as its corresponding formal argument.

Consider the below function definition:


C++
1
2
3
4
5
6
7

int addnumbers(int a, int b)


{
//body of the function enclosed in curly braces
int c;
c = a + b;
return c;
}

We can access the above defined function as shown below:


C++
1 int returnedvalue = addnumbers(2, 4);

In the above function call, values 2 and 3 are called actual arguments.

Tags:

access functions in C++

accessing functions in C++

YOU MAY ALSO LIKE...


0

Default Arguments in Functions

Function Prototypes in C++

12 OCT, 2013

6 OCT, 2013

LEAVE A REPLY
Name *

Email *

Website

+ nine = 12
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Function Prototypes in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Defining of Functions in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

FUNCTION PROTOTYPES IN C++ / FUNCTIONS IN C++

Function Prototypes in C++


BY LEARNCPPONLINE OCTOBER 6, 2013

All the functions in C++ (which are used first and defined later in the program) need to be prototyped.
Function prototypes are usually written at the beginning of the program, ahead of any programmer-defined functions (including

main() ).

The general form of a function prototype is:


C++
1 data-type function_name(type 1 arg 1, type 2 arg 2, ..., type n arg n);

Where,
data-type represents the data-type of the item that is returned by the function,
function_name represents the function name, and
type 1, type 2, , type n represents the data-type of the arguments arg 1, arg 2, , arg n.
Notice that a function prototype resembles the first line of a function definition (though a function prototype ends with a semicolon).
Advantages of Function Prototypes in C++:
Use of function prototypes in C++ offers following advantages:
1. Prototypes enables the compilers to provide stronger type checking
2. Because of the use of prototypes, the compiler can find and report any questionable type conversions between the arguments used to call a function and
the types of its (function;s) parameters
3. Because of the use of prototypes, the compiler can also catch differences between the number of arguments used to call a function and the number of
parameters in the functions
4. Function prototypes help to trap bugs before they occur
5. Function prototypes help to verify that the program is working correctly by not allowing functions to be called with mismatched arguments

Tags:

function prototypes

function prototypes in C++

YOU MAY ALSO LIKE...

prototype in C++

prototyping in C++

Function Overloading in C++

Defining of Functions in C++

12 OCT, 2013

6 OCT, 2013

1 RESPONSE

Comments 1

Pingbacks 0

nitish kumar October 13, 2014 at 3:33 am


Sir advanage of function prototype didnt understand very well. please more explain afp.
Reply

LEAVE A REPLY
Name *

Email *

Website

4 + five =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Default Arguments in Functions

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Accessing of Functions in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

FUNCTIONS IN C++

Default Arguments in Functions


BY LEARNCPPONLINE OCTOBER 12, 2013

In C, if a function is defined to receive 2 arguments, whenever we call this function we have to pass 2 values to this function. If we pass one value then some
garbage value is assumed for the last argument. As against this, functions in C++ have an ability to define default values for arguments that are not passed
when the function call is made. The default arguments are given only in the function prototype and should not be repeated in the function definition. The
compiler uses the prototype information to build a call, not the function definition.

Let us illustrate this concept with an example:


Here is an example of a prototype (i.e., function declaration) with default values:
C++
1 float amount(float principal, int period, float rate = 0.15);

The default value is specified in a manner syntactically similar to the variable initialization. The above prototype declares a default value of 0.15 to the argument
rate .
A subsequent function call like:
C++
1 value = amount(5000, 7);

passes the value 5000 to

principal

//one argument missing

and 7 to

period

and then lets the function use the default value of 0.15 for

rate .

One more important point to note here is that only the trailing arguments can have default values. That is, we must add defaults from right to left. We cannot
provide a default values to a particular argument in the middle of the argument list.
Hence, the prototypes:
C++
1 float amount(float principal = 5000, int period = 7, float rate);
2 float amount(float principal = 5000, int period, float rate = 0.15);

are invalid as they does not add defaults from right to left.

Tags:

arguments in functions

default arguments

default arguments in C++

YOU MAY ALSO LIKE...


1

Function Prototypes in C++

Defining of Functions in C++

6 OCT, 2013

6 OCT, 2013

LEAVE A REPLY
Name *

Email *

Website

= thirty

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Inline Functions in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Function Prototypes in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

FUNCTIONS IN C++

Inline Functions in C++


BY LEARNCPPONLINE OCTOBER 12, 2013

As we know, when we call a function from the main program, the control jumps to the function and then comes back to the calling program when execution of
function is completed. This process, although looks simple, but involves a lot of background processes as of passing of parameters to the function, allocating of
storage for functions local variables, storing the current variables, etc.
C++ provides a convenient way of omitting all these processes by simply declaring the function to be inline.

When we declare a function inline, we tell the compiler to replace each call to such function(declared inline) with the explicit code of the function. To declare a
function inline all we need to do is to add the word inline before the definition of the function as:
C++
1 inline data-type function_name(type 1 arg 1, type 2 arg 2, ..., type n arg n);

Also, in the function-prototype of the inline function, we are required to add the word

inline

before the prototype as:


C++

1 inline data-type function_name(type 1 arg 1, type 2 arg 2, ..., type n arg n);

Now, let us illustrate inline function with a simple example as follows:


Consider the following program where an inline function cube() is defined before

main

(hence it will not require function prototype) and then is called in

main :

C++
1
2
3
4
5
6
7
8
9
10
11
12

#include<iostream.h>
inline int cube(int x)
{
//returns cube of x
return x*x*x;
}
int main()
{
cout << cube(4) <<endl;
}

The above program will actually be compiled as follows:


C++
1 int main()
2 {
3 cout << (4)*(4)*(4) << endl;
4 }

Limitation of using inline functions:


Use of the inline functions can also cause negative side effects. For example, inlining a 40-line function that is called in 26 different locations would add at least

1000 lines of unnoticed source code to your program. Inlined functions can also limit the portability of your code across platforms.

Tags:

inline function

inline function in C++

inline functions

inline functions in c++

YOU MAY ALSO LIKE...


0

Accessing of Functions in C++

Default Arguments in Functions

6 OCT, 2013

12 OCT, 2013

4 RESPONSES

Comments 4

Pingbacks 0

nitish kumar October 13, 2014 at 3:42 am


Tanks ,its very helpful for me.
Reply
nitish kumar October 13, 2014 at 3:44 am
Sir why weuse function inline
Reply
Prashant N. November 2, 2014 at 2:40 pm
As we know, when we call a function from the main program, the control jumps to the function and then comes back to the calling program when execution of
function is completed. This process, although looks simple, but involves a lot of background processes as of passing of parameters to the function, allocating of
storage for functions local variables, storing the current variables, etc.

C++ provides a convenient way of omitting all these processes by simply declaring the function to be inline. When we declare a function inline, we tell the compiler
to replace each call to such function(declared inline) with the explicit code of the function.
So basically, if we use inline, it will minimize the number of function calls and thus minimizing the number of background processes
Hope this answers your query
Reply
Mark December 9, 2014 at 4:52 pm
Ive learn some good stuff here. Certainly worth bookmarking for revisiting.
I surprise how a lot attempt you put to create the sort of fantastic informative website.
Reply

LEAVE A REPLY
Name *

Website

Email *

nine 4 =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Function Overloading in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords

Default Arguments in Functions

Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.


About Us | Copyright Notice | Privacy Policy | Website Disclaimer
LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

FUNCTIONS IN C++

Function Overloading in C++


BY LEARNCPPONLINE OCTOBER 12, 2013

One significant addition made to the capabilities of function in C++ is that of function overloading. With this facility you can have multiple functions with the
same name, unlike C, where all the functions in a program must have unique names.
In C, every function has to have a unique name. At times this becomes annoying. For example, there are following three different functions that return the
absolute value of an argument:
C++
1 int abs (int i);
2 long labs (long l);
3 double fabs (double d);

All these functions do the same thing, so it seems unnecessary to have three different function names. C++ overcomes this situation by allowing the
programmer to create three different functions with the same name. This is called function overloading.

Let us illustrate function overloading with an example as follows:


C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

int max (int, int);


int max (int, int, int); //function prototypes
int main()
{
cout << max(99, 77) << " " << max(55, 66, 33);
}
//function defined
int max(int x, int y)
{
//returns maximum of two given integers
return (x > y ? x : y);
}
int max(int x, int y, int z)
{
//returns maximum of three given integers
int m = (x > y ? x : y);
return (z > m ? z : m);
}

Here, two different functions, all named max are defined. The compiler checks their parameter lists to determine which one to use on each call. For example,
the first call [max(99, 77)] passes two ints, so the version that has two ints in its parameter list is being called.

Tags:

function overloading

function overloading in C++

overloading function

YOU MAY ALSO LIKE...


0

Defining of Functions in C++

Default Arguments in Functions

6 OCT, 2013

12 OCT, 2013

LEAVE A REPLY
Name *

Email *

Website

seven = 14
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Arrays in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Inline Functions in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

ARRAYS IN C++ / FUNDAMENTALS OF C PLUS PLUS (C++)

Arrays in C++
BY LEARNCPPONLINE OCTOBER 12, 2013

An array can be defined as a sequence of data in memory, wherein all the data are of the same type, and are placed in physically adjacent locations.
An array is a group of elements that share a common name, and that are differentiated from one another by their positions within the array. For example, if we
have five numbers, all of which are named as x, they may be listed as:

The position of each of these elements can be indicated by means of a subscript as:
x1 = 65
x2 = 33
x3 = 64
x4 = 88
x5 = 5
In mathematics, a subscript is a number written to the right of the variable name, slightly below the line, and usually in small type. The subscript indicates the
position of the particular elements.
In C++, square brackets are used for this purpose.
The method of subscripting arrays in C++ is different from that used in many other programming languages. The elements of a five-element array in C++ are
numbered starting with 0, not with 1. Therefore, the assignment statements for this above example actually should be written as:
x[0] = 65;
x[1] = 33;
x[2] = 64;
x[3] = 88;
x[4] = 5;
Also, an array must be declared, since it is a type of variable. An array containing five elements, all of which are integers, can be declared as follows:
int x[5];
Defining of 1-Dimensional Arrays in C++:
In general terms, a 1-Dimensional array definition may be represented as:
C++
1 storage-class data-type array[expression];

Where

refers to the storage class of the array,


data-type is the data type,
array is the array name, and
expression is a positive-valued integer expression which indicates the number of array elements.

The

storage-class

storage-class

is optional; default values are

automatic

for arrays that are defined within a function or a block, and

external

for arrays that are defined

The
is optional; default values are
outside of a function.

for arrays that are defined within a function or a block, and

for arrays that are defined

Defining of Multi-Dimensional Arrays in C++:


In general terms, Multi-Dimensional array definition may be represented as:
C++
1 storage-class data-type array[expression 1][expression 2]...[expression n];

Where

refers to the storage class of the array,


data-type is the data type,
array is the array name, and
expression 1, expression 2,..., expression n are positive-valued integer expression that indicates the number of array elements associated with each
subscript.
storage-class

Remember that the storage-class is optional; default values are


that are defined outside of a function.

automatic

for arrays that are defined within a function or a block, and

external

for arrays

Also, you can refer the below links to get more details about arrays.
1-Dimensional Array
2-Dimensional Array

Tags:

1-dimentional array in C++

arrays

arrays in C++

multi-dimensional arrays in C++

multidimensional array

YOU MAY ALSO LIKE...


0

Operators and Expressions in C++

Type Conversion in C++

11 AUG, 2013

11 AUG, 2013

LEAVE A REPLY
Name *

Email *

Website

eight 5 =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Structures in C++

PREVIOUS STORY

Function Overloading in C++

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

PROGRAMS IN C++

Write first C++ Program using Microsoft Visual Studio


BY LEARNCPPONLINE OCTOBER 13, 2013

This article will guide you to write first C++ program using Visual Studio 2008 and explain you step by step on how to create your first C++ program.
Follow the following simple steps and you should be able to create your first c++ program without any issues:

1. Open the Microsoft Visual Studio 2008 application. Click on File > New > Project as shown below:
2. On performing the above steps, following window will be displayed.
Under Project types: select Visual C++ > Win32.
At the right hand side, under Templates Select Win32 Console Application.
Enter the Project Name in the Name field, Location and the the Name of the Solution as desired and click on OK button.

3. On performing the above step, following window will be displayed:


Click on Next Button. Select Console application as Application type and under Additional options, select Empty project and click Finish.

4. On clicking Finish button, Project will be created and the same can be seen in Solution Explorer as below:
If you do not find Solution Explorer, then click on View > Solution Explorer or Press Ctrl + Alt + L.
5. Next step is to right click on Source Files folder under Project Name in Solution Explorer and select Add > New Item.
Select C++ File (.cpp).

Enter the name of file as desired and Click on Add button.

6. On performing the above step, file named first-c-plus-plus-file.cpp will be created under Source Files folder.Now, type the following code in the file firstc-plus-plus-file.cpp and save the file (ctrl + s).
C++
1
2
3
4
5
6
7
8

#include <iostream>
#include "conio.h"
using namespace std;
void main() {
cout << "This is my first C++ Program - by LearnCPPOnline.com" << endl;
getch();
}

7. We are done with writing our first C++ program. Our next step is to compile and run the program and check the output.
Now, Press F7 keyboard button or select Build > Build Solution.In the output tab (View > Output), you will see the below message if everything goes fine
without any errors.
1
2
3
4
5
6
7
8
9
10
11
12
13

1>------ Build started: Project: First-C-Plus-Plus-Program, Configuration: Debug Win32 -----1>Compiling...


1>first-c-plus-plus-file.cpp
1>Compiling manifest to resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Linking...
1>Embedding manifest...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Build log was saved at "file://c:First-C-Plus-Plus-ProgramFirst-C-Plus-Plus-ProgramDebugBuildLog.htm"
1>First-C-Plus-Plus-Program - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

8. The last step is to run the program and check the output in the output screen.
Click on Debug > Start Without Debugging or click Ctrl + F5
Congratulations! You have successfully created your first C++ program using Microsoft Visual Studio 2008. For other versions of Microsoft Visual Studio (e.g.
Microsoft Visual Studio 2012), steps should be similar to the one mentioned above. So what are you waiting for? Go ahead and create your first C++ Program
and share your valuable feedback.

Tags:

first c++ program

how to write first c++ program

program in c++

YOU MAY ALSO LIKE...


0

Understanding the program in C++

Program in C++ to display the name of the day in a week

13 OCT, 2013

14 JUN, 2014

LEAVE A REPLY
Name *

Website

five 8 =
Comment

Email *

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Understanding the program in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement

Structures in C++

if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

PROGRAMS IN C++

Understanding the program in C++


BY LEARNCPPONLINE OCTOBER 13, 2013

In this article we will understand the program written in C++ programming language in step-by-step manner. Consider the below simple C++ program that
performs multiplication of two integers and displays the result.
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

/*Program that performs multiplication of two


integers and displays the result*/
//Include header files
#include <iostream>
#include "conio.h"
using namespace std;
//main program starts from here
void main() {
//declare variables
int result, variable1, variable2;
//assign values to variable1 and variable2
variable1 = 10;
variable2 = 2;
/*Perform multiplication of variable1 and variable2.
Store the result in the variable named result*/
result = variable1 * variable2;
//Display the result in the output window
cout << "The result is: " << result << endl;
}

Result of the above program:


MS DOS
1 The result is: 20

Let us understand the above program in detail:


Line 1 thru 2:
This is a multiline comment. We have used it to describe the purpose of the program which is a very good programming practice
Line 4:
This is a single line command.
Note: Comments are ignored by the compilers
Line 5 thru 7:
These are the header files that needs to be included in order to run the program successfully. These header files contains components/supporting
components/functions needed by the inbuilt functions like cout, cin etc. for its execution.
#include is known as pre-processor directives. It tell the compiler to include text from another file, stuffing it right into your source code. The whole
statement #include tells the compiler to take text from the file iostream.h and stick it into your source code before the source code is compiled.
Line 10:
This is the main function of the program. The program execution starts from this function. The statement that belongs to main() are enclosed within a pair
of braces {}
Line 12:
This line declares the integer variables to be used in the program. Any variable that we need to use must be declared before using it
Line 15 thru 16:
These two lines of code have been used to assign a value to the variables declared in the line 12
Line 21
Here, values stored in the variables named variable1 and variable2 are multiplied and the result is stored in the variable named result
Line 24:

Line 24:
This line uses inbuilt function cout to display the result of the multiplication in a specified format. endl is an inbuilt function that ends the current line
and takes the cursor to the next line.

Tags:

understand c++

understanding c++ program

understanding program

understanding program in C++

YOU MAY ALSO LIKE...


0

Program in C++ to read three numbers and compute their average

Program in C++ to display the name of the day in a week

14 JUN, 2014

14 JUN, 2014

LEAVE A REPLY
Name *

Email *

Website

five = 25
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Defining of Class in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Write first C++ Program using Microsoft Visual Studio

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF CLASSES AND OBJECTS IN C++

Defining of Class in C++


BY LEARNCPPONLINE OCTOBER 26, 2013

Class in C++ is almost similar to structures. The only difference being that classes have functions too, as their members, apart from different types of variables.
There is also one another difference between classes and structures. Such difference is that the members of a class are private. by default, while, the
members of a structure are public, by default. The meanings of private members and public members will get clearer to you as move ahead with the tutorial.
Defining or Declaration of Class:
A class is a way to bind the data and its associated functions together. It allows the data (and functions) to be hidden, if necessary, from external use. While
defining a class, we are creating a new abstract data type that can be treated like any other built-in data type.

Generally, a class specification in C++ has two parts:


1. Class declaration
2. Class function definitions
The class declaration describes the type and scope of its members. The class function definition describe how the class functions are implemented.
The general form of a class declaration is:
C++
1 class class_name
2
{
3
private:
4
variable
5
function
6
public:
7
variable
8
function
9
};

declaration;
declaration;
declaration;
declaration;

Now, let us understand the above definition in detail:


1.
2.
3.
4.

The keyword class specifies that what follows is an abstract data of type class_name
The body of the class is enclosed within braces and is terminated by a semicolon
The class body contains the declaration of variables and functions, collectively called as members
They are usually grouped under two sections, namely, private and public to denote which of the members are private and which of them are public. The
keywords private and public are known as visibility labels. Note that these keywords are followed by a colon.
The members that have been declared as private can be accessed only from within the class. On the other hand, public members can be accessed from
outside the class also. The data hiding (using private declaration) is the key feature of object-oriented programming. The use of the keyword private is
optional. By default, all the members of a class are private
5. The variables declared inside the class are known as data members and the functions are known as member functions. Only the member functions can
have access to private data members and private member functions. However, public members can be accessed from outside the class

6. The binding of data and functions together into a single class-type variable is referred to as encapsulation
Example of a class declaration:
C++
1 class item
2
{
3
//variable declaration
4
//private by default
5
int number;
6
float cost;
7
public:
8
//function declaration using prototype
9
void getdata(int a, int b);
10
void putdata(void);
11
};

Here,
item is a class name,
number and cost are data members which are private by default, and
getdata and putdata are the member functions and both are public.

Tags:

c++ class

class declaration

class definition

class in C++

classes and objects in C++

classes in C++

objects in C++

YOU MAY ALSO LIKE...


1

Class Objects as Function Arguments (Parameters)

Static Member Functions of Class in C++

21 DEC, 2013

15 DEC, 2013

LEAVE A REPLY
Name *

Email *

Website

= fourteen

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Defining of Member Functions of Class in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement

Understanding the program in C++

do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF CLASSES AND OBJECTS IN C++

Defining of Member Functions of Class in C++


BY LEARNCPPONLINE DECEMBER 14, 2013

Member functions can be defined at any one of the following two places:

1. Outside the Class


2. Inside the Class
It is obvious that, irrespective of the place of definition, the function should perform the same task. Therefore, the code for the function body would be identical
in both the cases. However, there is a subtle difference in the way the function header is defined. Both these approaches are discussed in detail as follows:
1. Outside the Class Definition:
Member functions that are declared (with the help of prototypes) inside a class have to be defined separately outside the class. Their definitions are very
much like the normal functions. They should have a function header and a function body.However, an important difference between a member function
and a normal function is that a member function incorporates a membership Identity label in the header. This labels tells the compiler which class the
function belongs to. The general form of a member function definition outside the class is:
C++
1 return-type class-name::function-name(argument declaration)
2
{
3
function-body
4
}

The membership label class-name:: tells the compiler that the function function-name belongs to the class class-name . That is, the scope of the
function is restricted to the class-name specified in the header line. The symbol :: is called the scope resolution operator.
For instance, consider the member functions

getdata()

and

putdata() . They may be coded (outside the class item) as follows:

C++
1 void item
2
{
3
4
5
}
6
7 void item
8
{
9
10
11
}

:: getdata(int a, float b)
number = a;
cost = b;
:: putdata()
cout << "Number :" << number << endl;
cout << "Cost :" << cost << endl;

Since, these functions do not return any value, their return-type is void.
Note:
1) Several different classes can use the same function name. The membership label will resolve their scope

2) Member functions can access the private data of the class. A non-member function cannot do so. (However, an exception to this rule is a friend function
discussed later)
3) A member function can call another member function directly, without using the dot operator (The use of dot operator in case of class is discussed
later)
2. Inside the Class Definition:
Another method of defining a member function is to replace the function declaration by the actual function definition inside the class. For example, we
could define the item class as follows:
C++
1 class item
2
{
3
int number;
4
float cost;
5
public:
6
void getdata(int a, float b)
//declaration
7
void putdata()
//definition
8
{
//inline function
9
cout << "Number :" << number << endl;
10
cout << "Cost :" << cost << endl;
11
}

When a function is defined inside a class, it is treated as an inline function. Therefore, all the restrictions and limitations that apply to an inline function are
also applicable here.

Tags:

defining member functions

defining member functions in C++

inside the class definition

member functions

member functions in C++

member functions of class

outside the class definition

YOU MAY ALSO LIKE...


0

Private Member Functions in C++

Accessing Class Members in C++

15 DEC, 2013

14 DEC, 2013

1 RESPONSE

Comments 1

Pingbacks 0

pediatra September 17, 2014 at 4:32 am


Good write-up. I certainly love this site. Thanks!
Reply

LEAVE A REPLY
Name *

Email *

Website

two 7 =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Creating Objects of Classes in C++

PREVIOUS STORY

Introduction to C++

Defining of Class in C++

Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.


About Us | Copyright Notice | Privacy Policy | Website Disclaimer
LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF CLASSES AND OBJECTS IN C++

Creating Objects of Classes in C++


BY LEARNCPPONLINE DECEMBER 14, 2013

Remember that the declaration of item class as shown before does not define any objects (i.e., variables) of item but only specifies what they will contain.
Once a class has been declared, we can create an variable of that type by using the class name (like any other built-in type variable).

For example:
C++
1 item x;

creates a variable x of type item . In C++, the class variables are known as objects.
Therefore, x is called an object of type item . We may also declare more than one object in one statement link:
C++
1 item x, y, z;

The declaration of the object is similar to that of a variable of any basic type.
Objects can also be created when a class is defined by placing their names immediately after the closing brace, as we do in the case of structures. That is, the
definition:
C++
1 class item
2
{
3
...
4
...
5
}x,y,z;

would create the objects x, y and z of type


used.

Tags:

create object in C++

item . This practice is seldom followed because we would like to declare the objects close to the place where they are

create object of class

YOU MAY ALSO LIKE...

creating objects in C++

creating objects of classes

objects of classes in C++

Private Member Functions in C++

Accessing Class Members in C++

15 DEC, 2013

14 DEC, 2013

LEAVE A REPLY
Name *

Email *

Website

= eight

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Accessing Class Members in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Defining of Member Functions of Class in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF CLASSES AND OBJECTS IN C++

Accessing Class Members in C++


BY LEARNCPPONLINE DECEMBER 14, 2013

As pointed out earlier, the private data of a class can be accessed only through the member functions of that class. The
access number and cost (of class item ) directly.
However, the member functions which are public, can be accessed from

main()

cannot contain statements that

main() . The following is the format for calling a public member function:

C++
1 object-name.function-name(actual arguments);

For example,
C++
1 x.getdata(100,75.6);

The function call statement (in case of class item discussed above) is valid and assigns the value 100 to number and 75.6 to cost of the object x by
implementing the getdata() function. The assignments occur in the actual function.
Similarly, the statement:
C++
1 x.putdata()

would display the values of data members.


Remember, a member function can be invoked only by using an object of the same class. Hence the statement like:
C++
1 getdata(100, 78.9);

has no meaning.
Similarly, the statement:
C++
1 x.number = 100;

is also illegal. This is because although x is an object of the type


member function and not by the object directly.

item

to which

number

belongs, the

number

(declared private) can be accessed only through a

It may be noted that objects communicate by sending and receiving messages. This is achieved through member functions. For example:
C++
1 x.putdata();

sends a message to the object x requesting it to display its contents.

Tags:

access members

access members of class

accessing class members

YOU MAY ALSO LIKE...


0

Static Data Members in C++

Private Member Functions in C++

15 DEC, 2013

15 DEC, 2013

LEAVE A REPLY
Name *

Email *

Website

+ three = 11
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Private Member Functions in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Creating Objects of Classes in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF CLASSES AND OBJECTS IN C++

Private Member Functions in C++


BY LEARNCPPONLINE DECEMBER 15, 2013

Although it is normal practice to place all the data items in a private section and all the functions in public, some situations may require certain functions to be
hidden (like private data) from the outside calls. Tasks such as deleting an account in a customer file, or providing an increment to an employee are events of
serious consequences and therefore the functions handling such tasks should have restricted access. We can place these functions in the private section of a
class.

A private member function can only be called by another function that is a member of its class. Even an object cannot invoke a private function using the dot
operator. For example, consider a class as defined below:
C++
1 class sample
2
{
3
private:
4
int m;
5
void read();
6
public:
7
void update();
8
void write();
9
};

If

a1

is an object of class

//private member function

sample , then:

C++
1 a1.read();

is illegal. However, the function

read()

can be called by the public member function

update()

to the read/update the value of

as:
C++

1 void sample::update()
2
{
3
read();
4
}

Tags:

member functions C++

private member function

YOU MAY ALSO LIKE...

private member functions in C++

private members

Accessing Class Members in C++

Defining of Member Functions of Class in C++

14 DEC, 2013

14 DEC, 2013

LEAVE A REPLY
Name *

Email *

Website

one 8 =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Static Data Members in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Accessing Class Members in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF CLASSES AND OBJECTS IN C++

Static Data Members in C++


BY LEARNCPPONLINE DECEMBER 15, 2013

A data member of a class can be qualified as static (storage-class). The properties of a static member variable are similar to that of a C static variable. Read
more about storage classes in C (Click here)

In addition, a static member variable (of a class) has certain special characteristics which are detailed as follows:
1. It is initialized to zero when the first object of its class is created. No other initialization is permitted
2. Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created
3. It is visible only within the class, but its lifetime is the entire program
Static variables are normally used to maintain values common to the entire class. For example, a static data member can be used as a counter that records the
occurrences of all the objects. Program given below illustrates the use of a static data member.
C++
1 #include <iostream.h>
2 class item
3
{
4
private:
5
static int count;
//count is STATIC
6
int number;
7
public:
8
void getdata(int a)
9
{
10
number = a;
11
count++;
12
}
13
void getcount()
14
{
15
cout << "Count :" << count << endl;
16
}
17
};
18
19
int item::count;
//count DEFINED
20
21
void main()
22
{
23
item a, b, c;
//count is initialized to zero
24
a.getcount();
//display count
25
b.getcount();
26
c.getcount();
27
28
a.getdata(100);
//getting data into object a
29
b.getdata(220);
//getting data into object b
30
c.getdata(550);
//getting data into object c
31
32
cout << "After reading data" << endl;
33
34
a.getcount();
//display count
35
b.getcount();

36
37

c.getcount();
}

The output of the above program would be:


C++
1
2
3
4
5
6
7
8
9

count: 0
count: 0
count: 0
After reading data
count: 3
count: 3
count: 3

In the given program:


1. Notice the following statement:
int item::count;

Note that the type and scope of each static member variable must be defined outside the class definition. This is necessary because the static data
members are stored separately rather than as a part of an object. Since they are associated with the class itself rather than with any class objects, they are
also known as class variable
2. The static variable count is initialized to zero when the first objects is created. The count is incremented whenever the data is read into an object. Since the
data is read into object three times, the variable count is incremented three times. Because, there is only one copy of count shared by all the three objects,
all the three output statements cause the value 3 to be displayed
3. Static variables are like non-inline member functions in the sense that they are declared in a class definition and defined outside the source file

Tags:

Learn C++ Online

static data members

static data members in C++

YOU MAY ALSO LIKE...


1

Defining of Member Functions of Class in C++

Array of Objects in C++

14 DEC, 2013

21 DEC, 2013

LEAVE A REPLY
Name *

Email *

Website

3+
Comment

= nine

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Static Member Functions of Class in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives

Private Member Functions in C++

Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF CLASSES AND OBJECTS IN C++

Static Member Functions of Class in C++


BY LEARNCPPONLINE DECEMBER 15, 2013

Like static member variable, we can also have static member functions in a class. A member function that is declared static has the following properties:

1. A static member function can have access to only other static members (functions or variables) declared in the same class
2. A static member function can be called using the class name (instead of its objects) as follows:
C++
1 class-name :: function-name;

Program below illustrates the implementation of these characteristics:


C++
1 #include <iostream.h>
2 class test
3
{
4
private:
5
int code;
6
static int count;
//static member variable
7
public:
8
void setcode()
9
{
10
code = ++count;
11
}
12
void showcode()
13
{
14
cout << "Object number: " << code << endl;
15
}
16
static void showcount()
//static member function
17
{
18
cout << "Count: " << count << endl;
19
}
20
};
21
22
int test :: count;
23
24
void main()
25
{
26
test t1, t2;
27
28
t1.setcode();
29
t2.setcode();
30
31
test :: showcount();
//accessing static function
32
33
test t3;
34
t3.setcode();
35
36
test :: showcount();
//accessing static function
37
38
t1.showcode();
39
t2.showcode();

39
40
41

t2.showcode();
t3.showcode();
}

Output of the above program would be:


C++
1
2
3
4
5

count:
count:
Object
Object
Object

2
3
number: 1
number: 2
number: 3

In the above program:


1.
2.
3.
4.

The static member function showcount() displays the number of objects created till that moment
A count of number of objects created is maintained by the static variable count
The function showcode() displays the code number of each object
Note that the statement:
code=++count;

is executed whenever setcode() function is invoked and current value of


value contained in code represents a unique number of its object

Tags:

Learn C++ Online

static member functions

static member functions in C++

count

is assigned to

code . Since each object has its own copy of code , the

static members

YOU MAY ALSO LIKE...


0

Private Member Functions in C++

Accessing Class Members in C++

15 DEC, 2013

14 DEC, 2013

LEAVE A REPLY
Name *

Website

eight + 5 =
Comment

Email *

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Class Objects as Function Arguments (Parameters)

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement

Static Data Members in C++

for loop statement


while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF CLASSES AND OBJECTS IN C++

Class Objects as Function Arguments (Parameters)


BY LEARNCPPONLINE DECEMBER 21, 2013

Like any other data-type, an object may be used as a function argument. This can be done in two ways:
1. A copy of the entire object is passed to the function
2. Only the address of the object is transferred to the function
The first method is called pass-by-value. Since a copy of the object is passed to the function, any changes made to the object inside the function do not affect
he object used to call the function.
The second method is called pass-by-reference. When an address of the object is passed, the called function works directly on the actual object used in the
call. This means that nay changes made to the object inside the function will reflect in the actual object. The pass-by-reference method is more efficient since
it requires to pass only the address of the object and not the entire object.

Program given below illustrates the use of objects as function arguments. It performs the addition of time in the hour and minutes format.
C++
1 #include <iostream.h>
2 class time
3
{
4
private:
5
int hours;
6
int minutes;
7
public:
8
void gettime(int h, int m)
9
{
10
hours = h;
11
minutes = m;
12
}
13
void puttime()
14
{
15
cout << hours << " hours and ";
16
cout << minutes << " minutes " << endl;
17
}
18
void sum(time, time);
//function prototype
19
//Objects as arguments
20
};
21
22
void time :: sum(time t1; time t2)
//t1 and t2 are objects
23
{
24
minutes = t1.minutes + t2.minutes;
25
hours = minutes/60;
26
minutes = minutes%60;
27
hours = hours + t1.hours + t2.hours;
28
}
29
30
void main()
31
{
32
time T1, T2, T3;

33
34
35
36
37
38
39
40
41

T1.gettime(2, 45);
T2.gettime(3, 30);

//get T1
//get T2

T3.sum(T1, T2);

//get T3 = T1 + T2

cout << "T1 = " << T1.puttime();


cout << "T2 = " << T2.puttime();
cout << "T3 = " << T3.puttime();

//display T1
//display T2
//display T3

The output of the above program would be:


T1 = 2 hours and 45 minutes
T2 = 3 hours and 30 minutes
T3 = 6 hours and 15 minutes
In the above program:
1. Since the member function sum() is invoked by the object T3, with the objects T and T2 as arguments, it can directly access the hours and minutes
variables of T3
2. But, the members of T1 and T2 can be accessed only by using the dot operator (like T1.hours and T1.minutes). Therefore, inside the function sum(), the
variables hours and minutes refer to T3. T1.hours and T1.minutes refer to T1, and T2.hours and T2.minutes refer to T2
3. An object can also be passed as an argument to a non-member function. However, such functions can have access to the public member functions only
through the objects passed as arguments to it. These non-member functions cannot have access to the private data members

Tags:

Class Objects as Arguments

Class Objects as Function Parameters

Learn C++ Online

objects as arguments

objects as parameter

YOU MAY ALSO LIKE...


0

Friend Functions and Friend Classes in C++

Static Data Members in C++

21 DEC, 2013

15 DEC, 2013

1 RESPONSE

Comments 1

Pingbacks 0

pawan sonawane July 30, 2015 at 3:24 pm


not executed that program well be change in program function
Reply

LEAVE A REPLY
Name *

Website

Email *

two

=1

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Friend Functions and Friend Classes in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords

Static Member Functions of Class in C++

Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.


About Us | Copyright Notice | Privacy Policy | Website Disclaimer
LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF CLASSES AND OBJECTS IN C++

Friend Functions and Friend Classes in C++


BY LEARNCPPONLINE DECEMBER 21, 2013

We have been emphasizing that the private members cannot be accessed from outside the class. That is, a non-member function cannot have an access to
the private data of a class. However, there could be a situation where we would like two classes to share a particular function. For example, consider a case
where two classes, manager and scientist , have been defined. We would like to use a function income() to operate on the objects of both these classes. In
such situations, C++ allows the common function to be made friendly with both the classes, thereby allowing the function to have access to the private data of
these classes. Such a function need not be a member of any of these classes.
To make an outside class friendly to a class, we have to simply declare this function as a friend of the class as illustrated below:
C++
1 class ABC
2
{
3
private:
4
...
5
...
6
7
public:
8
...
9
...
10
11
friend void xyz();
12
};

//declaration

The function declaration should be preceded by the keyword friend/ The function can de defined elsewhere in the program like a normal C++ function. The
function definition does not use either the keyword friend or the scope operator ::. The functions that are declared with the keyword friend are known as
friend function.
A friend function, although not a member function, has full access rights to the private members of the class.

A friend function possesses certain special characteristics s stated below:


1. It is not in the scope of the class to which it has been declared as friend
2. Since it is not in the scope of the class, it cannot be called using the object of that class. It can be invoked like a normal function without the help of any
object
3. Unlike member functions, it cannot access the member names directly and has to use an object name and dot membership operator with each member
name (e.g., C.x)
4. It can be declared either in the public or the private part of a class without affecting its meaning
5. Usually, it has the objects as arguments
Program given below illustrates the use of a friend function.
C++

1 #include <iostream.h>
2 class sample
3
{
4
private:
5
int a;
6
int b;
7
public:
8
void setvalue()
9
{
10
a = 25;
11
b = 40;
12
}
13
friend float mean(sample s);
//Friend declared
14
};
15
float mean(sample s)
16
{
17
return float(s.a + s.b)/2.0;
18
}
19
20
void main()
21
{
22
sample x;
23
x.setvalue();
24
cout << "Mean value = " << mean(x) << endl;
25
}

The output of the above program would be:


Mean value = 32.5
In the given program:
1. Note that the friend function accesses the class variables a and b by using the dot operator and the object passed to it
2. The function call mean(x) passes the object x by value to the friend function
Now, also, member functions of one class can be friend functions of another class. In such cases, they are defined using the scope resolution operator as
illustrated below:
C++
1 class X
2
{
3
4
5
6
7
};
8
9 class Y
10
{
11
12
13
14
15
};

...
...
int fun1()
...

//member function of class X

...
...
friend int X::fun1();
...

//fun1() of X is a friend of Y

In the above illustrative skeletal, the function fun1() is a member of class X and a friend of class Y.
We can also declare all the member functions of one class as the friend of another class. In such cases, the class is called as a friend class. This can be illustrated
as follows:
C++
1 class Z
2
{
3
4
5
6
7
};

Tags:

...
...
friend class X;
...

friend class

friend class in C++

//all member functions of X are friends to Z

friend function

friend function in C++

friend functions and friend classes

Learn C++ Online

YOU MAY ALSO LIKE...


0

Static Member Functions of Class in C++

Creating Objects of Classes in C++

15 DEC, 2013

14 DEC, 2013

LEAVE A REPLY
Name *

Email *

Website

nine +

= 16

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Array of Objects in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Class Objects as Function Arguments (Parameters)

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF CLASSES AND OBJECTS IN C++

Array of Objects in C++


BY LEARNCPPONLINE DECEMBER 21, 2013

We know that an array can be of any data type. Hence, we can also have arrays of variables that are of the type class. Such variables are called array of objects.
Consider the following class definition:
C++
1 class employee
2
{
3
char name[30];
4
float age;
5
public:
6
void getdata();
7
void putdata();
8
};

The identifier employee is a user-defined data type and can be used to create objects that relate to different categories of the employee.
For example, consider the following:
C++
1 employee managers[3]; //array of manager
2 employee foremen[15]; //array of foreman
3 employee workers[75]; //array of worker

In above declaration, the array managers contains three objects (managers), namely, managers[0], managers[1] and managers[2], of the type employee class.
Similarly, the foremen array contains 15 objects (foremen) and the workers array contains 75 objects (workers).
Since, an array of objects behave like any other array, we can use the usual array-accessing methods to access individual elements and then the dot member
operator to access the member functions.
For example, the statement:
manager[i].putdata();

will display the data of the ith element of the array managers. That is, this statement requests the object manager[i] to invoke the member function putdata().

Tags:

array objects

array of objects

Learn C++ Online

object arrays

YOU MAY ALSO LIKE...


0

Friend Functions and Friend Classes in C++

Static Data Members in C++

21 DEC, 2013

15 DEC, 2013

LEAVE A REPLY
Name *

Email *

Website

7 five =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Why use Constructor and Destructor in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Friend Functions and Friend Classes in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF CONSTRUCTORS AND DESTRUCTORS IN C++

Why use Constructor and Destructor in C++


BY LEARNCPPONLINE DECEMBER 21, 2013

We have seen, so far, a few examples of classes being implemented. In all the cases, we have used member functions such as getdata() and setvalue() to
provide initial values to the private member variables. For example, the following statement:
X.input();
invokes the member function input(), which assigns initial values to the data items of the object X. Similarly, the statement:
A.getdata(100,299.95);
passes the initial values as arguments to the function getdata(), where these values are assigned to the private variables of object A. All these function call
statements are used with the appropriate objects that have already been created. These functions cannot be used to initialise the member variables at the
time of creation of their objects.
This means that we are not able to initialize a class type variable (object) when it is declared, much the same way as initialization of an ordinary variable. For
example:
int p = 20;
float q = 20.5;
are the valid initialization statements for basic data type.
But, such statements have not happened with the objects we have so far studied.
It is therefore clear that some more features of classes need to be explored that would enable us to initialize the objects when they are created and destroy
them when their presence is no longer necessary.

C++ provides a special member function called the constructor which enables an object to initialize itself when it is created. This is known as automatic
initialization of objects. It also provides another member function called the destructor that destroys the objects when they are no longer required.

Tags:

Constructor and Destructor

Constructor in C++

Destructor in C++

Use of Constructor and Destructor

why use constructor

why use destructor

YOU MAY ALSO LIKE...


0

Parameterized Constructors in C++

Constructors in C++

11 JAN, 2014

26 DEC, 2013

LEAVE A REPLY
Name *

Email *

Website

5 = fifteen
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Constructors in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Array of Objects in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF CONSTRUCTORS AND DESTRUCTORS IN C++

Constructors in C++
BY LEARNCPPONLINE DECEMBER 26, 2013

A constructor in C++ is a special member function whose task is to initialize the objects of its class. It is special because its name is the same as the class name.
The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of data members of the
class.
A constructor is declared and defined as follows:
C++
1 class integer
//class with a constructor
2
{
3
private:
4
int m, n;
5
public:
6
integer();
//constructor declared
7
...
8
...
9
};
10
11 integer :: integer()
//constructor defined
12
{
13
m = 0;
14
n = 0;
15
}

When a class contains a constructor like the one defined above, it is guaranteed that an object created by the class will be initialized automatically.

For example, the declaration:


C++
1 integer in1;

//object in1 created

not only creates the object in1 of type integer but also initializes its data members m and n to zero. There is no need to write any statement to invoke the
constructor function (as we do with the normal member functions). If a normal member function is defined for zero initialization, we would need to invoke this
function for each of the object separately. This would be very inconvenient, if there are a large number of objects.
The constructor functions in C++ have some special characteristics as stated below:
1.
2.
3.
4.

They should be declared in the public section


They are invoked automatically when the objects are created
They do not have return types, not even void and therefore, they cannot return values
They cannot be inherited, though derived class can call the base class constructor (The topics of inheritance, derived class and base class would be covered
later)
5. Like other C++ functions, they can have default arguments
6. Constructors cannot be virtual (Meaning of virtual would be discussed later)

7. We cannot refer to their addresses


Remember, when a constructor is declared for a class, initialization of the class objects becomes mandatory.

Tags:

constructor

Constructor in C++

constructors

constructors in C++

Learn C++ Online

YOU MAY ALSO LIKE...


0

Constructor with Default Arguments in C++

Why use Constructor and Destructor in C++

12 JAN, 2014

21 DEC, 2013

2 RESPONSES

Comments 0

Pingbacks 2

LEAVE A REPLY
Name *

Email *

Website

= seven

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Parameterized Constructors in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Why use Constructor and Destructor in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF CONSTRUCTORS AND DESTRUCTORS IN C++

Parameterized Constructors in C++


BY LEARNCPPONLINE JANUARY 11, 2014

The constructor integer(), defined in the previous section (Constructors in C++), initializes the data members of all the objects to zero. However, in practice it
may be necessary to initialize the various data elements of different objects with different values when the are created. C++ permits us to achieve this
objective by passing arguments to the constructor function when the objects are created. The constructors that can take the arguments are called
parameterized constructors.
The constructor integer() may be modified to take arguments as shown below:
C++
1 class integer
2
{
3
private:
4
int m, n;
5
public:
6
integer(int x, int y);
7
...
8
...
9
};
10
11 integer :: integer(int x, int y)
12
{
13
m = x;
14
n = y;
15
}

//parameterized constructor

When a constructor has been parameterized, the object declaration statement such as:
C++
1 integer in1;

may not work. We must pass the initial values as arguments to the constructor function when an object is declared. This can be done in two ways:
1. By calling the constructor explicitly, or
2. By calling the constructor implicitly
The following declaration illustrates the first method (explicitly):
C++
1 integer in1 = integer(0,100);

//explicit call

This statement creates an integer object in1 and passes the value 0 and 100 to it.
Now, the second method(implicit) is depicted below:
C++
1 integer in1(0, 100);

//implicit call

This method, sometimes called the shorthand method, is used very often as it looks shorter, looks better and is easy to implement.

Remember, when the constructor is parameterized, we must provide appropriate argument for the constructor.
Program given below demonstrates the passing of arguments to the constructor functions:
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#include<iostream.h>
class integer
{
private:
int m, n;
public:
integer(int, int);
//constructor declared
void display(void)
{
cout << "m = " << m << endl;
cout << "n = " << n << endl;
}
};
integer :: integer(int x, int y)
{
m = x;
n = y;
}

//constructor defined

void main()
{
integer in1(0, 100);
//IMPLICIT call
integer in2 = integer(23,76);
//EXPLICIT call
cout << "Object1" << endl;
in1.display();
cout << "Object2" << endl;
in2.display();
}

The output of the above program would be:


C++
1
2
3
4
5
6
7

Object1
m = 0
n = 100
Object2
m = 23
n = 76

Now, the constructor functions can also be defined as inline functions as illustrated below:
C++
1 class integer
2
{
3
private:
4
int m, n;
5
public:
6
integer(int x, int y)
7
{
8
m = x;
9
n = y;
10
}
11
...
12
...
13
};

//inline constructor

Also, the parameters of a constructor can be of any type except that of the class to which it belongs. For example:
C++
1 class A
2
{
3
4
5

private:
...
...

6
7
8

public:
A(A);
};

//Is ILLEGAL

The above example is ILLEGAL.


However, a constructor can accept a reference to its own class as a parameter. For example:
C++
1 class A
2
{
3
4
5
6
7
8
}

private:
...
...
public:
A(&A);
//Is VALID

The above example is VALID.


In such cases, the constructor is called the copy constructor.

Tags:

Learn C++ Online

Parameterized Constructors

Parameterized Constructors in C++

YOU MAY ALSO LIKE...


0

Why use Constructor and Destructor in C++

Constructors in C++

21 DEC, 2013

26 DEC, 2013

LEAVE A REPLY
Name *

Website

5 + one =
Comment

Post Comment

Email *

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Constructor with Default Arguments in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement

Constructors in C++

do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF CONSTRUCTORS AND DESTRUCTORS IN C++

Destructors in C++
BY LEARNCPPONLINE JANUARY 25, 2014

A destructor in C++, as the name implies, is used to destroy the objects that have been created by a constructor. Like a constructor, the destructor in C++ is a
member function whose name is the same as the class name but is preceded by a tilde (~). For example, the destructor for the class integer can be defined
as shown below:
C++
1 ~integer()
2
{
3
4
}

A destructor in C++ never takes any arguments nor does it return any value. It will be invoked implicitly by the compiler upon exit from the program (or the
block or the function as the case may be) to clean up the storage that is no longer accessible. It is a good practice to declare destructors in a program since it
releases memory space for future use.
Whenever

new

is used to allocate memory in the constructors, we should use

Note:

and

delete

Tags:

new

delete

to free that memory.

operators would be discussed later.

Constructor and Destructor

Destructor in C++

destructors

destructors in C++

how to use destructor in C++

how to use destructors

Learn C++ Online

YOU MAY ALSO LIKE...


0

Why use Constructor and Destructor in C++

Constructors in C++

21 DEC, 2013

26 DEC, 2013

LEAVE A REPLY
Name *

Email *

Website

+ 3 = twelve
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Defining of Derived Classes in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance

Constructor with Default Arguments in C++

Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++

Introduction to Inheritance in C++


BY LEARNCPPONLINE JULY 2, 2015

Let me introduce you to the concept of Inheritance in C++.


Reusability is yet another important feature of C++. It is always nice if we could reuse something that already exists rather than trying to create the same all
over again. It would not only save the time and money but also reduce frustration and increase readability. For instance, the reuse of a class that has already
been tested, debugged and used many time can save us the effort of developing and testing the same again.
Fortunately, C++ strongly supports the concept of reusability.The C++ classes can be reused in several ways. Once a class has been written and tested, it can be
adapted by other programmers to suit their requirements. This is basically done by creating new classes which reuse the properties of the existing ones. The
mechanism of deriving a new class from an old one is called inheritance in C++ (or derivation). The old class is referred to as the base class and the new one is
called the derived class.

The derived class inherits some or all of the traits from the base class. A class can also inherit properties from more than once class or from more than one
level.
A derived class with only one base class is called single inheritance and one with several base classes is called multiple inheritance. On the other hand, the traits
of one class may be inherited by more than one class. This process is known as hierarchical inheritance. The mechanism of deriving a class from another
derived class is known as multilevel inheritance. The above figure shows various forms of inheritance in C++ that could be used for writing extensible
programs.

Tags:

define inheritance

inheritance types

definition of inheritance

intro to inheritance

hierarchical inheritance

Learn C++ Online

hybrid inheritance

multilevel inheritance

inheritance

multiple inheritance

inheritance in C++

single inheritance

inheritance meaning

inheritance means

types of inheritance

YOU MAY ALSO LIKE...


3

Multi-Level Inheritance in C++

Defining of Derived Classes in C++

1 FEB, 2014

26 JAN, 2014

LEAVE A REPLY
Name *

Email *

Website

four 3 =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Introduction to C++ Programming

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

sizeof operator in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++

Defining of Derived Classes in C++


BY LEARNCPPONLINE JANUARY 26, 2014

How do we define a derived class in C++


A derived class is defined by specifying it relationship with the base class in addition to its own details.
The general form of defining a derived class is:
C++
1 class derived-class-name : visibility-mode base-class-name
2
{
3
...
4
...
//members of derived class
5
...
6
};

where,
class is the required keyword,
derived-class-name is the name given to the derived class,
base-class-name is the name given to the base class,
: (colon) indicates that the derived-class-name is derived from the base-class-name,
visibility-mode is optional and, if present, may be either private or public. The default visibility-mode is private. Visibility mode specifies whether the features
of the base class are privately derived or publicly derived.
Examples:
C++
1 class ABC : private XYZ
//private derivation
2
{
3
members of ABC
4
};
5
6 class ABC : public XYZ
//public derivation
7
{
8
members of ABC
9
};
10
11 class ABC : XYZ
//private derivation by default
12
{
13
members of ABC
14
};

When a base class is privately inherited by a derived class, public members of the base class becomes private members of the derived class and therefore the
public members of the base class can only be accessed by the member functions of the of the derived class. They are inaccessible to the objects of the derived
class.
Remember, a public member of the class can be accessed by its own objects by using the dot operator. The result is that no member of the base class is
accessible to the objects of the derived class in case of private derivation.
On the other hand, when the base class is publicly inherited, public members of the base class becomes public members of the derived class and therefore
they are accessible to the objects of the derived class.
In both the cases, the private members are not inherited and therefore, the private members of a base class will never become the members of its derived
class.

In inheritance, some of the base class data elements and member functions are inherited into the derived clas. We can also add our own data and member
functions and thus extend the functionality of the base class. Inheritance, when used to modify and extend the capabilities of the existing classes, becomes a
very powerful tool for incremental program development.

Tags:

Derived Classes

Derived Classes in C++

how to derive class from base class

derived classes use

Learn C++ Online

derived classes visibility mode

private inheritance

how derived classes work

how to define derived class

public inheritance

YOU MAY ALSO LIKE...


2

Single Inheritance Public Inheritance in C++

Single Inheritance Private Inheritance in C++

29 JAN, 2014

29 JAN, 2014

LEAVE A REPLY
Name *

Website

+ eight = 15
Comment

Email *

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Single Inheritance Public Inheritance in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement

Destructors in C++

for loop statement


while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++ / PRIVATE AND PUBLIC INHERITANCE IN C++ /
SINGLE INHERITANCE IN C++

Single Inheritance Public Inheritance in C++


BY LEARNCPPONLINE JANUARY 29, 2014

Inheritance in which the derived class is derived from only one base class is called single inheritance.
Let us consider a simple example to illustrate the single inheritance. Program given below shows a base class B and a derived class D. The class B contains one
private data member, one public data member and three public member functions. The class D contains one private data member and two public member
functions.
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

#include<iostream.h>
class B
{
private:
int a;
//private; not inheritable
public:
int b;
//public; ready for inheritance
void get_ab();
int get_a();
void show_a();
};
class D : public B
//public derivation
{
private:
int c;
public:
void mul();
void display();
};
/*...Defining of functions...*/
void B :: get_ab()
{
a = 5;
b = 10;
}
int B :: get_a()
{
return a;
}
void B :: show_a()
{
cout << "a = " << a <<endl;
}
void D :: mul()
{
c = b * get_a();
}
void D :: display()
{
cout << "a = " << get_a() << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
/*...Main Program...*/
void main()
{
D x;
x.get_ab();

60
61
62
63
64
65
66
67

x.mul();
x.show_a();
x.display()
x.b = 20;
x.mul();
x.display();
}

Given below is the output of the above program:


C#
1
2
3
4
5
6
7
8

a
a
b
c

=
=
=
=

5
5
10
50

a = 5
b = 20
c = 100

In the above program, following things require to be noted:

1. The class D is a public derivation of the base class B. Therefore, class D inherits all the public members of class B an retains their visibility. Thus, a public
member of the base class B is also a public member of the derived class D
2. The private members of the base class B cannot be inherited by the derived class D
3. The class D, in effect, will have more members than what it contains at the time of declaration
4. The program illustrates that the objects of class D have access to all the public members of class B
Let us have a look at the function show_a() and mul():
C++
1 void B :: show_a()
2
{
3
cout &lt;&lt; "a = " &lt;&lt; a &lt;&lt;endl;
4
}
5
6 void D :: mul()
7
{
8
c = b * get_a();
9
}

Although the data member a is a private member of base class B and cannot be inherited, objects of derived class D are able to access it through an inherited
member function of class B.

Tags:

demonstrate single inheritance with example

single inheritance

single inheritance with example

example of single inheritance

explain single inheritance with an example

understand public inheritance with example

Learn C++ Online

public inheritance

understand single inheritance

YOU MAY ALSO LIKE...


0

Defining of Derived Classes in C++

Single Inheritance Private Inheritance in C++

26 JAN, 2014

29 JAN, 2014

2 RESPONSES

Comments 0

Pingbacks 2

LEAVE A REPLY
Name *

Email *

Website

9 one =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Single Inheritance Private Inheritance in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Defining of Derived Classes in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++ / PRIVATE AND PUBLIC INHERITANCE IN C++ /
SINGLE INHERITANCE IN C++

Single Inheritance Public Inheritance in C++


BY LEARNCPPONLINE JANUARY 29, 2014

Inheritance in which the derived class is derived from only one base class is called single inheritance.

Let us consider a simple example to illustrate the single inheritance. Program given below shows a base class B and a derived class D. The class B contains one
private data member, one public data member and three public member functions. The class D contains one private data member and two public member
functions.
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include<iostream.h>
class B
{
private:
int a;
//private; not inheritable
public:
int b;
//public; ready for inheritance
void get_ab();
int get_a();
void show_a();
};
class D : public B
//public derivation
{
private:
int c;
public:
void mul();
void display();
};
/*...Defining of functions...*/
void B :: get_ab()
{
a = 5;
b = 10;
}
int B :: get_a()
{
return a;
}
void B :: show_a()
{
cout << "a = " << a <<endl;
}

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

void D :: mul()
{
c = b * get_a();
}
void D :: display()
{
cout << "a = " << get_a() << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
/*...Main Program...*/
void main()
{
D x;
x.get_ab();
x.mul();
x.show_a();
x.display()
x.b = 20;
x.mul();
x.display();
}

Given below is the output of the above program:


C#
1
2
3
4
5
6
7
8

a
a
b
c

=
=
=
=

5
5
10
50

a = 5
b = 20
c = 100

In the above program, following things require to be noted:


1. The class D is a public derivation of the base class B. Therefore, class D inherits all the public members of class B an retains their visibility. Thus, a public
member of the base class B is also a public member of the derived class D
2. The private members of the base class B cannot be inherited by the derived class D
3. The class D, in effect, will have more members than what it contains at the time of declaration
4. The program illustrates that the objects of class D have access to all the public members of class B
Let us have a look at the function show_a() and mul():
C++
1 void B :: show_a()
2
{
3
cout &lt;&lt; "a = " &lt;&lt; a &lt;&lt;endl;
4
}
5
6 void D :: mul()
7
{
8
c = b * get_a();
9
}

Although the data member a is a private member of base class B and cannot be inherited, objects of derived class D are able to access it through an inherited
member function of class B.

Tags:

demonstrate single inheritance with example

single inheritance

single inheritance with example

example of single inheritance

explain single inheritance with an example

understand public inheritance with example

Learn C++ Online

public inheritance

understand single inheritance

YOU MAY ALSO LIKE...


1

Protected Inheritance in C++

Multi-Level Inheritance in C++

30 JAN, 2014

1 FEB, 2014

2 RESPONSES

Comments 0

Pingbacks 2

LEAVE A REPLY
Name *

Email *

Website

five +

=7

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Single Inheritance Private Inheritance in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Defining of Derived Classes in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++ / PRIVATE AND PUBLIC INHERITANCE IN C++ /
SINGLE INHERITANCE IN C++

Single Inheritance Private Inheritance in C++


BY LEARNCPPONLINE JANUARY 29, 2014

Let us understand what does private inheritance mean


Consider a simple example to illustrate the single inheritance. The Program given below shows a base class B and a derived class D. The class B contains one
private data member, one public data member and three public member functions. The class D contains one private data member and two public member
functions.
C++
1 #include<iostream.h>
2
3 class B
4
{
5
private:
6
int a;
//private; not inheritable
7
public:
8
int b;
//public; ready for inheritance
9
void get_ab();
10
int get_a();
11
void show_a();
12
};
13
14 class D : private B
//private derivation
15
{
16
private:
17
int c;
18
public:
19
void mul();
20
void display();
21
};

In private derivation, the public members of the base class becomes private members of the derived class. Therefore, the objects of the derived class D
cannot have direct access to the public member functions of the base class B.

Hence, if we create an object of the derived class D,


C++
1 D x;

The statements such as:


C++
1 x.get_ab(); //get_ab() is private
2 x.get_a(); //get_a() is private
3 x.show_a(); //show_a() is private

will not work. However, these functions can be used inside mul() and display() like the normal functions as shown below:
C++
1 void mul()
2
{
3
get_ab();
4
c = b * get_a();
5
}
6
7 void display()
8
{
9
show_a(); //outputs value of a
10
cout << "b = " << b << endl;
11
cout << "c = " << c << endl;
12
}

Tags:

demonstrate private inheritance with example

single inheritance

example of private inheritance

example of single inheritance

inheritance in C++

Learn C++ Online

private inheritance

understand private inheritance with example

YOU MAY ALSO LIKE...


1

Protected Inheritance in C++

Single Inheritance Public Inheritance in C++

30 JAN, 2014

29 JAN, 2014

LEAVE A REPLY
Name *

Email *

Website

= one

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Protected Inheritance in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Single Inheritance Public Inheritance in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++ / PROTECTED INHERITANCE IN C++

Protected Inheritance in C++


BY LEARNCPPONLINE JANUARY 30, 2014

Understanding the concept of Protected Inheritance in C++.


A special mention of visibility label protected:
We have just seen how to increase the capabilities of an existing class without modifying it. We have also seen that a private member of a base class cannot be
inherited and therefore its is not available for the derived class directly. What do we do if the private data needs to be inherited by a derived class? This can be
accomplished by modifying the visibility limit of the private member by making it public. But, this would make it accessible to all the other functions of the
program, thus, eliminating the advantage of data hiding.

C++ provides a third visibility modifier, protected which serves a limited purpose in inheritance. A member declared as protected is accessible by the member
functions within its class and any class immediately derived from it. It cannot be accessed by the function outside these two classes. A class can now use all the
three visibility modes as illustrated below:
C++
1 class alpha
2
{
3
private:
4
...
5
...
6
7
protected:
8
...
9
...
10
public:
11
...
12
...
13
};

//optional
//visible to the member functions
//within its class
//visible to the member functions
//of its own and derived class
//visible to all functions
//in the program

When a protected member is inherited in public mode, it becomes protected in the derived class too, and therefore is accessible by the member functions of
the derived class. It is also ready for the further inheritance.
A protected member, inherited in the private mode derivation, becomes private in the derived class. Although, it is available to the member functions of the
derived class, it is not available for further inheritance (since private members cannot be inherited).
The keywords private, public and protected may appear in any order and in any number of times in the declaration of a class.
For example:
C++
1 class beta
2
{
3
protected:
4
...
5
public:

6
7
8
9
10
11

...
private:
...
public:
...
}

is a valid class definition.


However, the normal practice is to use them as follows:
C++
1 class beta
2
{
3
... //private by default
4
...
5
protected:
6
...
7
public:
8
...
9
}

A special mention of Protected Inheritance:


In previous posts, we mentioned about defining of derived class by using keywords private and/or public as visibility mode. In addition to that, we can also use
the keyword protected as visibility mode in the defining of the derived class. Such inheritance using the visibility mode protected is known as protected
inheritance.
In protected inheritance, public members of base class become protected in derived class as also the protected members of the base class become protected
in the derived class. However, as usual, private members of a base class cannot be inherited.
Example:
C++
1 class ABC :: protected XYZ
2
{
3
members of ABC
4
};

Tags:

inheritance

inheritance in C++

//protected derivation

Learn C++ Online

protected inheritance

protected inheritance in C++

single inheritance

YOU MAY ALSO LIKE...


3

Multi-Level Inheritance in C++

Single Inheritance Private Inheritance in C++

1 FEB, 2014

29 JAN, 2014

1 RESPONSE

Comments 1

Pingbacks 0

bradford September 15, 2014 at 1:33 pm


I blog quite often and I seriously appreciate your content.
Your article has really peaked my interest.

I will bookmark your blog and keep checking for new details about once
a week. I subscribed to your Feed too.
Reply

LEAVE A REPLY
Name *

Email *

Website

four

=2

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Multi-Level Inheritance in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers

Single Inheritance Private Inheritance in C++

Accessing of the Pointer Variables


void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++ / MULTI-LEVEL INHERITANCE

Multi-Level Inheritance in C++


BY LEARNCPPONLINE FEBRUARY 1, 2014

The mechanism of deriving a class from another derived class is known as multi-level inheritance in C++

It is not uncommon that a class is derived from another derived class as shown below:

The class A serves as a base class for the derived class B which in turn serves as a base class for the derived class C. The class B is known as intermediate base
class since it provides a link for the inheritance between A and C. The chain A -> B -> C is known as inheritance path.
A derived class with multi-level inheritance is declared as follows:
C++
1 class A
//Base Class
2
{
3
...
4
};
5
6 class B : public A
//B derived from A
7
{
8
...
9
};
10
11 class C : public B
//C derived from B
12
{
13
...
14
};

This process can be extended to any number of levels.


Let us consider the below example:
Assume that the test results of a batch of students are stored in three different classes. class student stores the roll-number, class test stores the marks
obtained in two subjects and class result contains the total marks obtained in the test. The class result can inherit the details of the marks obtained in the
test and the roll-number of the students through multilevel inheritance.

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

class student
{
protected:
int roll_number;
public:
void get_number(int);
void put_number(void);
};
void student :: get_number(int a)
{
roll_number = a;
}
void student :: put_number()
{
cout << "Roll Number: " << roll_number << endl;
}
class test : public student
//First level derivation
{
protected:
float sub1;
float sub2;
public:
void get_marks(float, float);
void put_marks(void);
};
void test :: get_marks(float x, float y)
{
sub1 = x;
sub2 = y;
}
void test :: put_marks()
{
cout << "Marks in Sub1 = " << sub1 << endl;
cout << "Marks in Sub2 = " << sub2 << endl;
}
class result : public test //second Level Derivation
{
float total;
//private by default
public:
void display();
};

The last class

result

in the above example, after inheritance from B to C, would contain the following members:
C++

1 private:
2
float total;
//own member
3 protected:
4
int roll_number; //inherited from student via test
5
float sub1;
//inherited from test
6
float sub;
//inherited from test
7 public:
8
void get_number(int);
//from student via test
9
void put_number(void);
//from student via test
10
void get_marks(float, float); //from test
11
void put_marks(void);
//from test
12
void display(void);
//own member

The inherited functions put_numbers() and put_marks() can be used in the definition of display() function as follows:
C++
1 void result :: display(void)
2
{
3
total = sub1 + sub2;
4
put_number();
5
put_marks();
6
cout << "Total = " << total << endl;
7
}

Here is a simple main() program based on the above defined class result:
C++
1 void main()
2
{
3
result student;
//student1 object created
4
5
student1.get_number(786);
6
student1.get_marks(71.5, 99.5);
7
8
student1.display(); //display the result of student1
9
}

Tags:

example of multi level inheritance

multilevel inheritance program

inheritance in C++

Learn C++ Online

program to demonstrate multilevel inheritance

multi level inheritance

types of inheritance

multilevel inheritance

multilevel inheritance example

understand multi level inheritance with example

YOU MAY ALSO LIKE...


2

Single Inheritance Public Inheritance in C++

Multiple Inheritance in C++

29 JAN, 2014

1 FEB, 2014

3 RESPONSES

Comments 2

Pingbacks 1

Charle Madinga January 4, 2015 at 12:31 pm


I am new to C++so bear with me: does anybody how to output the (shortest) inheritance path between classes for example in the following multi-level inheritance:
John
|

||
James Jill
|
|-|
Susan Edward
And you want to output the inheritance path: Susan->James->John <-Jill
An example code would be be very helpful !!!
Reply
Charle Madinga January 4, 2015 at 12:36 pm
My apologies the hierarchy did not come out right. John is the base class; James and Jill are the derived classes from John and Susan & Edward in turn are derived from
James. How do you output for example:
Susan->James->John <-Jill
Reply

LEAVE A REPLY
Name *

Website

Email *

4 + five =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Multiple Inheritance in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations

Protected Inheritance in C++

Operators and Expressions


Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++ / MULTIPLE INHERITANCE IN C++

Multiple Inheritance in C++


BY LEARNCPPONLINE FEBRUARY 1, 2014

Inheritance in which a derived class is derived from several base class is known as multiple inheritance.
A class can inherit the attributes of two or more classes as shown in fig. below. This is known as multiple inheritance.

Multiple inheritance allows us to combine the features of several existing classes as a starting point for defining new classes. It is like a child inheriting the
physical features of one parent and the intelligence of another.

The syntax of a derived class with multiple base class is as follows:


C++
1 class D : visibility B-1, visibility B-2...
2 {
3
...
4
... (Body of D)
5
...
6 };

where visibility may be either public or private.


The base classes are separated by commas.
Example:
C++
1 class P : public M, public N
2
{
3
public:
4
void display(void);
5
};

Classes M and N have been specified as follows:


C++
1 class M
2
{
3
protected:
4
int m;

5
public:
6
void get_m(int);
7
};
8
9 void M :: get_m(int x)
10
{
11
m = x;
12
}
13
14 class N
15
{
16
protected:
17
int n;
18
public:
19
void get_n(int);
20
};
21
22 void N :: get_n(int y)
23
{
24
n = y;
25
}

The derived class P, as declared above would, in effect, contain all the members of M and N in addition to its own members as shown below:
C++
1 class P
2
{
3
protected:
4
int m;
//from the class M
5
int n;
//from the class N
6
public:
7
void get_m(int);
//from the class M
8
void get_n(int);
//from the class N
9
void display(void); //own member
10
};

The main() function of the program based on the above defined class P may be written as follows:
C++
1 main()
2
{
3
P x;
4
x.get_m(10);
5
x.get_n(15);
6
x.display();
7
}

Tags:

demonstrate multiple inheritance

understand multiple inheritance

example of multiple inheritance

inheritance in C++

Learn C++ Online

multiple inheritance

multiple inheritance with example

understand multiple inheritance with example

YOU MAY ALSO LIKE...


3

Multi-Level Inheritance in C++

Single Inheritance Public Inheritance in C++

1 FEB, 2014

29 JAN, 2014

1 RESPONSE

Comments 0

Pingbacks 1

LEAVE A REPLY
Name *

Email *

Website

4 = two
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Basics of Pointers in C++

PREVIOUS STORY

Multi-Level Inheritance in C++

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions

this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

POINTERS IN C++

Basics of Pointers in C++


BY LEARNCPPONLINE FEBRUARY 20, 2014

Before we start with basics of pointers, it is strictly recommended to go through the concepts of pointers from the Learn C Online (Click here). We have
attempted to cover pointers in the most simplest manner possible. But, it will be impossible for you to grasp the knowledge of the pointers unless and until
you have the concepts of pointers clear in your mind by reading Learn C Online (Click here).
A pointer is a variable that represents the location (rather than the value) of a data item, such as a variable or an array element.
Suppose v is a variable that represents some particular data item. The compiler will automatically assign memory cells for this data item. The data item can
then be accessed if we know the location (i.e. the address) of the first memory cell allocated to variable v by the compiler. The address of v s memory location
can be determined by the expression:
C++
1 &v

where & is a unary operator, called the address operator, that evaluates the address of the operand.

Now, let us assign the address of

to another variable,

pv , Thus:

C++
1 pv = &v

The new variable (pv) is called a pointer to v, since it points to the location where v is stored in memory. Remember, however, that pv represents vs
address, not its value. Thus, pv is referred to as pointer variable. The data item represented by v (i.e., the data item stored in the vs memory cells) can be
accessed by the expression:
C++
1 *pv

where * is a unary operator, called the indirection operator.

Tags:

c++ pointer

c++ pointers

YOU MAY ALSO LIKE...

Learn C++ Online

pointer c++

pointers

pointers c

Void Pointers in C++

Pointers to functions in C++

27 FEB, 2014

8 MAR, 2014

LEAVE A REPLY
Name *

Email *

Website

+ 7 = nine
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Declaration of Pointers in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Multiple Inheritance in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

POINTERS IN C++

Declaration of Pointers in C++


BY LEARNCPPONLINE FEBRUARY 23, 2014

Pointer variables, like all other variables, must be declared before they may be used in a C++ program. The interpretation of a pointer declaration differs,
however, from the interpretation of other variable declarations.
When a pointer variable is declared, the variable name must be preceded by an asterisk (*). This identifies the fact that the variable is a pointer.
The data type that appears in the declaration refers to the object of the pointer, i.e., the data item that is stored in the address represented by the pointer,
rather than the pointer itself.
This, the pointer declaration may be written in general terms as:
data-type *ptvar;

where,
ptvar is the name of the pointer variable, and
data-type refers to the datatype of the pointers object.
Remember that an * (asterisk) must precede ptvar.

Tags:

c pointer variables

pointer declaration in C++

declaration of pointer

declaration of pointer variables

pointer variable declaration in c++

pointer variables

declaration of pointers

declaration of pointers in C++

pointer variables declaration

pointers declaration

Learn C++ Online

pointer declaration

pointers declaration in C++

YOU MAY ALSO LIKE...


1

this pointer in C++

Basics of Pointers in C++

3 JUL, 2014

20 FEB, 2014

LEAVE A REPLY
Name *

Email *

Website

4 seven =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Accessing of the Variables pointed to by the Pointer

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance

Basics of Pointers in C++

Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

POINTERS IN C++

Accessing of the Variables pointed to by the Pointer


BY LEARNCPPONLINE FEBRUARY 27, 2014

Accessing of the variable pointed to by the pointer needs to be understood from two angles, i.e.,
1. Accessing of normal basic data-type variables
2. Accessing of class-type objects
1. Accessing of normal basic data-type variables:
Let us straight-away learn the accessing of normal basic data-type (line

int , float , etc.) variables by seeing a simple example as follows:

C++
1
2
3
4
5

int *p;
int x;
x = 20;
p = &x; //read as assign address of x (&x) to pointer variable p
cout << *p; //read as print content at pointer variable p

The statement:
C++
1 cout << *p;

prints the value 20 because

*p

means the value at the address p.

Similarly:
C++
1 *p = *p + 10

is same as:
C++
1 x = x + 10;

Note that if we write the following statement:


C++
1 cout << p;

this prints the address of x. Similarly the statement:


C++
1 cout << &x;

also prints the addresss of x.


2. Accessing the class-type objects (Pointer to Object):
Now, let us understand in brief about accessing of members of an object pointed to by a pointer.Just as you can have pointers to other types of variables,
you can have pointers to objects too. When accessing members of a class given a pointer to an object, use the arrow -> operator instead of the dot
operator. The operator looks like an arrow and is formed from a minus sign and a greater than symbol.
The following program illustrates how to access an object given a pointer to it:
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

Tags:

#include <iostream.h>
class c1
{
private:
int i;
public:
c1(int j)
{
i = j;
}
int get_i()
{
return i;
}
};
int main()
{
c1 ob(88), *p;
p = &ob;
//get address of ob
cout << p -> get_i(); //use -> to call get_i()
}

accessing class object pointer

data type pointer

accessing of variable pointers

data-type variables pointer

Learn C++ Online

accessing pointer variables


Pointer to class object

accessing variables by pointer

Pointer to Object

class pointer

class-type objects pointers

pointer variable

YOU MAY ALSO LIKE...


1

this pointer in C++

Void Pointers in C++

3 JUL, 2014

27 FEB, 2014

LEAVE A REPLY
Name *

Website

Email *

6 nine =
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Void Pointers in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations

Declaration of Pointers in C++

Operators and Expressions


Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++
Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

POINTERS IN C++

Void Pointers in C++


BY LEARNCPPONLINE FEBRUARY 27, 2014

For understanding the concept of void pointers, we should note that we cannot assign the address of one data-type variable to a pointer of another data-type
variable. Consider the following:

Note that we can not assign the address of

float

type variable to a pointer to

int

as:
C++

1 float y;
2 int *p;
3 p = &y;

//illegal statement

Similarly, see the following:


C++
1 float *p;
2 int x;
3 p = &x; //illegal statement

That means, if variable-type and pointer-to-type is same then only we can assign the address of that variable to pointer variable. If both, i.e., the data-type of
the variable and the data-type pointed to by the pointer variable, are different then we can not assign the address of variable to pointer variable. But this
impossible thing is made possible in C++ by declaring pointer variables as void as follows:
C++
1 void *p;

The above pointer is called pointer to

void

type. That means pointer

can point to any type of data like

int , float , etc. For example:

CoffeeScript
1
2
3
4
5

void *p;
int x;
float y;
p = &x; //legal statement
p = &y; //legal statement

The above statement are perfectly fine in C++ and they depict the use of void pointers.

Tags:

pointer void in C

void in c++

void pointer

void pointer in c++

void pointers

YOU MAY ALSO LIKE...


2

Pointers to functions in C++

Declaration of Pointers in C++

8 MAR, 2014

23 FEB, 2014

LEAVE A REPLY
Name *

Email *

Website

+ 2 = eight
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Pointers to functions in C++

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Accessing of the Variables pointed to by the Pointer

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

POINTERS IN C++

Pointers to functions in C++


BY LEARNCPPONLINE MARCH 8, 2014

In C++, just as we can have pointers to variables, we can have pointers to functions as well.
The general syntax of a pointers to a functions is:
return-type (*pointer-name) (list of parameter);
For example:
C++
1 void (*p) (float, int);

In the above declaration, a pointer to a function returns

void

and takes the two formal arguments of

float

and

int

types respectively.

After declaring a pointer to a function, the address of the function must be assigned to a pointer as follows:
p = &function-name;
where p is a pointer variable.
Through pointer to function, the function is called as follows:
C++
1 A = (*p) (x, y);

where, x and y are actual parameters, and


A is a variable in which the functions return value is stored,
p is a pointer name which points to the function
Let us illustrate pointer to function with a programming example as follows:
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include<iostream.h>
void main()
{
float add(float, float); //function prototype
float a, b, s;
float (*p) (float, float); //declaration of pointer to function
p = &add;
//assigning function add to p
a = 20.5;
b = 10.3;
s = (*p) (a,b);

//calling function add through pointer


//to function p
cout << "s= " << s << endl;
}
float add(float a1, float a2)
{
//add function defined
int sum;
sum = a1 + a2;
return(sum);
}

The output of the above program would be:


s = 30.8

In the above program:


The statement:
C++
1 float (*p) (float, float);

declares a pointer
The statement:

which is a pointer to function

C++
1 p = &add;

assigns the address of function


The statement:

add

to p , i.e., now pointer

points to function

add

C++
1 s = (*p) (a, b);

calls the function which is pointed by p (i.e., add) and then assigns the returned value of function to s
It is worthwhile to note here that if in pointer to function declaration we skip the bracket in which pointer name is is written the C++ compiler interprets it
as a function which returns a pointer type value. For example, if in above program we write:
C++
1 float *p (float, float);

then this statement means

Tags:

example of pointer to functions

learn pointers to function

is a function which returns pointer and the pointer points to

func pointers c++

pointer functions c++

func to pointer

pointers to function

func to pointers

float

function pointers

pointers to functions in c++

type data. i.e., function returns a pointer to

function pointers c++

understand function pointers c++

float

Learn C++ Online


understand pointers to functions

YOU MAY ALSO LIKE...


0

Basics of Pointers in C++

this pointer in C++

20 FEB, 2014

3 JUL, 2014

2 RESPONSES

Comments 2

Pingbacks 0

fredrick September 2, 2014 at 6:14 pm


Hi! I could have sworn Ive been to this website before but after checking through some of the post I realized its new to me.
Anyhow, Im definitely happy I found it and Ill be
book-marking and checking back frequently!
Reply
Amit September 26, 2014 at 2:37 pm
Nice explanation, but Why VOID MAIN() it cant be accepted.
Reply

LEAVE A REPLY
Name *

Email *

Website

seven = 2
Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Program in C++ to read three numbers and compute their average

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Void Pointers in C++

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

Learn C++ Online

POINTERS IN C++

this pointer in C++


BY LEARNCPPONLINE JULY 3, 2014

C++ uses a unique keyword called


which this function was called.

this

to represent an object that invokes a member function.

this

pointer in C++ is a pointer that points to the object for

For example, the function call:


C++
1 A.max()

will set the pointer

this

to the address of the object A.

The starting address is the same as the address of the first variable in the class structure.
This unique pointer is automatically passed to a member function when it is called. The pointer

this

acts as an implicit argument to all the member functions.

Consider the following simple example:


C++
1 class ABC
2 {
3
int a;
4
...
5
...
6 };

The private variable

can be used directly inside a member function, like:


C++

1 a = 123;

We can also use the following statement to do the same job:


C++
1 this -> a = 123;

Since C++ permits the use of shorthand form

a = 123 , we have not been using the pointer this

explicitly so far.

Tags:

c++ this pointer

this pointer

this pointer c++

YOU MAY ALSO LIKE...


0

Basics of Pointers in C++

Accessing of the Variables pointed to by the Pointer

20 FEB, 2014

27 FEB, 2014

1 RESPONSE

Comments 1

Pingbacks 0

bob September 25, 2014 at 5:12 pm


Great great site!
I cant wait to read more.
Congratulations again, from France.
Reply

LEAVE A REPLY
Name *

Email *

Website

four

= 24

Comment

Post Comment

FOLLOW:

CATEGORIES

Accessing of functions in C++

Arrays in C++

Branching and Selection in C++

Breaking Control Statements in C++

Concepts of Classes and Objects in C++

Concepts of Constructors and Destructors in C++

Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Conditional Statements in C Plus Plus

Control Statements in C++

Decision-making statement in C++

Defining of functions in C++

Expressions in C++

Function prototypes in C++

Functions in C++

Fundamentals of C Plus Plus (C++)

Inheritance in C++

Input and Output in C++

Loop Statements in C++

Multi-Level Inheritance

Multiple Inheritance in C++

Operators in C++

Pointers in C++

Preprocessor Directives in C++

Private and Public Inheritance in C++

Programs in C++

Protected Inheritance in C++

Single Inheritance in C++

Type conversion in C++

MORE
NEXT STORY

Program in C++ to find weekday based on weekday number

PREVIOUS STORY

Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
Input and Output
Preprocessor Directives
Control Statements
if statement
if-else statement
switch Statement
for loop statement
while loop statement
do-while statement
break statement in C++
continue statement in C++
goto statement in C++

Program in C++ to find sum of first n natural numbers

Defining of Functions
Accessing of Functions
Function Prototypes in C++
Default Arguments in Functions
Inline Functions
Function Overloading
Arrays in C++
Write First C++ Program
Understand the Program
Defining of Class
Defining of Member Functions of Class
Creating Objects of Classes
Accessing Class Members
Private Member Functions
Static Data Members
Static Member Functions
Class Objects as Arguments
Friend Functions and Friend Classes
Array of Objects
Why use Constructor and Destructor
Constructors in C++
Parameterized Constructors
Destructors in C++
Introduction to Inheritance
Defining of Derived Classes
Single Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Multi-Level Inheritance
Multiple Inheritance
Basics of Pointers
Declaration of Pointers
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
this pointer in C++

Now Learn C++ Online absolutely free. Yes, its a Free, Online C++ programming tutorial site which will help you learn C++ online and provide you with the
detailed knowledge about C++ Object Oriented Concepts (OOPS concepts). It is a complete tutorial that covers C++ programming right from the basics up to
the advanced object oriented programming concepts.

About Us | Copyright Notice | Privacy Policy | Website Disclaimer


LearnCPPOnline.com 2016. All Rights Reserved.

You might also like