Learn C++ Online: Introduction To C++ Programming
Learn C++ Online: Introduction To C++ Programming
Learn C++ Online: Introduction To C++ Programming
CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++ 2 JUL, 2015
#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++.
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
#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
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs 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.
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:
C Plus Plus
C++
Introduction to C++
11 AUG, 2013
11 AUG, 2013
2 RESPONSES
Comments 2
Pingbacks 0
LEAVE A REPLY
Name *
Email *
Website
+ 5 = eight
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs 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
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.
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.
//
/*..*/
pair is
Tags:
comments in C++
11 AUG, 2013
11 AUG, 2013
LEAVE A REPLY
Name *
Email *
Website
3 = three
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT 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
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.
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:
11 AUG, 2013
10 AUG, 2013
1 RESPONSE
Comments 1
Pingbacks 0
LEAVE A REPLY
Name *
Email *
Website
seven +
= 11
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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,
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 .
false
int main()
{
bool flag = false;
cout << "flag = " << flag <<endl;
flag = true;
cout << "flag = " << flag << endl;
}
Output:
flag = 0
flag = 1
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:
17 AUG, 2013
11 AUG, 2013
3 RESPONSES
Comments 3
Pingbacks 0
LEAVE A REPLY
Name *
Email *
Website
two +
=6
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
First, the
expression
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
root1
and
root2
flag
is a char-type variables.
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;
Tags:
declarations in C++
factor
star
sum
is a floating-point
-6
variables in C++
11 AUG, 2013
5 JUL, 2015
LEAVE A REPLY
Name *
Email *
Website
7+
= nine
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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++
Logical operators
operators in C++
Unary operators
17 AUG, 2013
11 AUG, 2013
LEAVE A REPLY
Name *
Email *
Website
+ 4 = seven
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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.
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);
expression
to, and
expression
3 a = 200;
4 b = 400;
5 c = (float) a * b;
Tags:
is converted into
type casting
float
Arrays in C++
12 OCT, 2013
11 AUG, 2013
LEAVE A REPLY
Name *
Email *
Website
9 three =
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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 <<
<<
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 >>
>>
Above statement causes the value provided from the keyboard to get assigned to the variable n.
C++
1 cin >> n1;
2 cin >> n2;
3 cin >> n3;
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
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++
>>
Output in C++
5 JUL, 2015
11 AUG, 2013
LEAVE A REPLY
Name *
Email *
Website
four +
Comment
= 13
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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
#if, #else, #elif, #endif, #if defined (or #ifdef), #if !defined (or #ifndef)
#define, #undef
#line
#error
#paragma
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.
Structures in C++
13 OCT, 2013
11 AUG, 2013
1 RESPONSE
Comments 1
Pingbacks 0
LEAVE A REPLY
Name *
Email *
Website
6 = eighteen
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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
22 SEP, 2013
22 SEP, 2013
LEAVE A REPLY
Name *
Email *
Website
six
=4
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
If 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
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.
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.
if
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
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
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
BRANCHING AND SELECTION IN C++ / CONDITIONAL STATEMENTS IN C PLUS PLUS / CONTROL STATEMENTS IN C++ / DECISION-MAKING STATEMENT IN C++
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
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
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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.
BRANCHING AND SELECTION IN C++ / CONDITIONAL STATEMENTS IN C PLUS PLUS / CONTROL STATEMENTS IN C++ / DECISION-MAKING STATEMENT IN C++
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
switch (expression) {
case constant 1:
statement;
case constant 2:
statement;
..........
..........
case constant n:
statement;
default:
statement;
}
break
statement.
switch
break
switch
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
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
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
The
for
statement or
The
for
loop is the most commonly used statement in C++. This loop consists of three expression:
for
for
statement is:
C++
In other words,
C++
1
2
3
4
5
6
7
Where,
#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;
}
22 SEP, 2013
29 SEP, 2013
3 RESPONSES
Comments 2
Pingbacks 1
LEAVE A REPLY
Name *
Website
six + 9 =
Comment
Email *
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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
1 while(expression)
2 statement;
while
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:
while statement
5 OCT, 2013
16 SEP, 2013
5 RESPONSES
Comments 4
Pingbacks 1
LEAVE A REPLY
Name *
Email *
Website
2+
= six
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
PREVIOUS STORY
Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
Operators and Expressions
Type Conversion
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.
The
do-while
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
do-while statement
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
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
The
break
or a
switch
statement.
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 statement
break
15 SEP, 2013
22 SEP, 2013
LEAVE A REPLY
Name *
Email *
Website
9+
= fourteen
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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
The
continue
for , a while
or a
do-while
statement.
C++
1 continue;
Tags:
continue
is a keyword in the C++ program and the semicolon must be inserted after the
continue c++
continue statement
continue
statement.
15 SEP, 2013
16 SEP, 2013
LEAVE A REPLY
Name *
Email *
Website
= one
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
The
goto
statement is used to alter the normal sequence of the program execution by transferring control to some other part of the program.
goto
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
15 SEP, 2013
5 OCT, 2013
3 RESPONSES
Comments 3
Pingbacks 0
LEAVE A REPLY
Name *
Email *
Website
two 7 =
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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
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:
defining functions
function in c++
12 OCT, 2013
12 OCT, 2013
LEAVE A REPLY
Name *
Email *
Website
five +
Comment
=6
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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.
In the above function call, values 2 and 3 are called actual arguments.
Tags:
12 OCT, 2013
6 OCT, 2013
LEAVE A REPLY
Name *
Email *
Website
+ nine = 12
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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() ).
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
prototype in C++
prototyping in C++
12 OCT, 2013
6 OCT, 2013
1 RESPONSE
Comments 1
Pingbacks 0
LEAVE A REPLY
Name *
Email *
Website
4 + five =
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
FUNCTIONS IN C++
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.
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);
principal
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
6 OCT, 2013
6 OCT, 2013
LEAVE A REPLY
Name *
Email *
Website
= thirty
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
FUNCTIONS IN C++
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
1 inline data-type function_name(type 1 arg 1, type 2 arg 2, ..., type n arg n);
main
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;
}
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 functions
6 OCT, 2013
12 OCT, 2013
4 RESPONSES
Comments 4
Pingbacks 0
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
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
FUNCTIONS IN C++
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.
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
overloading function
6 OCT, 2013
12 OCT, 2013
LEAVE A REPLY
Name *
Email *
Website
seven = 14
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs 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++
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.
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
The
storage-class
storage-class
automatic
external
The
is optional; default values are
outside of a function.
Where
automatic
external
for arrays
Also, you can refer the below links to get more details about arrays.
1-Dimensional Array
2-Dimensional Array
Tags:
arrays
arrays in C++
multidimensional array
11 AUG, 2013
11 AUG, 2013
LEAVE A REPLY
Name *
Email *
Website
eight 5 =
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
Structures 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
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.
PROGRAMS IN C++
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.
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).
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
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:
program in c++
13 OCT, 2013
14 JUN, 2014
LEAVE A REPLY
Name *
Website
five 8 =
Comment
Email *
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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.
PROGRAMS IN C++
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
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 program
14 JUN, 2014
14 JUN, 2014
LEAVE A REPLY
Name *
Email *
Website
five = 25
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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.
declaration;
declaration;
declaration;
declaration;
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 in C++
objects in C++
21 DEC, 2013
15 DEC, 2013
LEAVE A REPLY
Name *
Email *
Website
= fourteen
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
Member functions can be defined at any one of the following two places:
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
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:
member functions
15 DEC, 2013
14 DEC, 2013
1 RESPONSE
Comments 1
Pingbacks 0
LEAVE A REPLY
Name *
Email *
Website
two 7 =
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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;
Tags:
item . This practice is seldom followed because we would like to declare the objects close to the place where they are
15 DEC, 2013
14 DEC, 2013
LEAVE A REPLY
Name *
Email *
Website
= eight
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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()
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()
has no meaning.
Similarly, the statement:
C++
1 x.number = 100;
item
to which
number
belongs, the
number
It may be noted that objects communicate by sending and receiving messages. This is achieved through member functions. For example:
C++
1 x.putdata();
Tags:
access members
15 DEC, 2013
15 DEC, 2013
LEAVE A REPLY
Name *
Email *
Website
+ three = 11
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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
sample , then:
C++
1 a1.read();
read()
update()
as:
C++
1 void sample::update()
2
{
3
read();
4
}
Tags:
private members
14 DEC, 2013
14 DEC, 2013
LEAVE A REPLY
Name *
Email *
Website
one 8 =
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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();
}
count: 0
count: 0
count: 0
After reading data
count: 3
count: 3
count: 3
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:
14 DEC, 2013
21 DEC, 2013
LEAVE A REPLY
Name *
Email *
Website
3+
Comment
= nine
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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;
39
40
41
t2.showcode();
t3.showcode();
}
count:
count:
Object
Object
Object
2
3
number: 1
number: 2
number: 3
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;
Tags:
count
is assigned to
code . Since each object has its own copy of code , the
static members
15 DEC, 2013
14 DEC, 2013
LEAVE A REPLY
Name *
Website
eight + 5 =
Comment
Email *
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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
//display T1
//display T2
//display T3
Tags:
objects as arguments
objects as parameter
21 DEC, 2013
15 DEC, 2013
1 RESPONSE
Comments 1
Pingbacks 0
LEAVE A REPLY
Name *
Website
Email *
two
=1
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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.
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
}
...
...
int fun1()
...
...
...
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 function
15 DEC, 2013
14 DEC, 2013
LEAVE A REPLY
Name *
Email *
Website
nine +
= 16
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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
object arrays
21 DEC, 2013
15 DEC, 2013
LEAVE A REPLY
Name *
Email *
Website
7 five =
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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 in C++
Destructor in C++
Constructors in C++
11 JAN, 2014
26 DEC, 2013
LEAVE A REPLY
Name *
Email *
Website
5 = fifteen
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs 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++
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.
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.
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.
Tags:
constructor
Constructor in C++
constructors
constructors 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
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
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();
}
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
private:
...
...
public:
A(&A);
//Is VALID
Tags:
Parameterized Constructors
Constructors in C++
21 DEC, 2013
26 DEC, 2013
LEAVE A REPLY
Name *
Website
5 + one =
Comment
Post Comment
Email *
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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.
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
Note:
and
delete
Tags:
new
delete
Destructor in C++
destructors
destructors in C++
Constructors in C++
21 DEC, 2013
26 DEC, 2013
LEAVE A REPLY
Name *
Email *
Website
+ 3 = twelve
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++
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
hybrid inheritance
multilevel inheritance
inheritance
multiple inheritance
inheritance in C++
single inheritance
inheritance meaning
inheritance means
types of inheritance
1 FEB, 2014
26 JAN, 2014
LEAVE A REPLY
Name *
Email *
Website
four 3 =
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++
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
private inheritance
public inheritance
29 JAN, 2014
29 JAN, 2014
LEAVE A REPLY
Name *
Website
+ eight = 15
Comment
Email *
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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++
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.
CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++ / PRIVATE AND PUBLIC INHERITANCE IN C++ /
SINGLE INHERITANCE IN C++
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();
}
a
a
b
c
=
=
=
=
5
5
10
50
a = 5
b = 20
c = 100
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 << "a = " << a <<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:
single inheritance
public inheritance
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
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++ / PRIVATE AND PUBLIC INHERITANCE IN C++ /
SINGLE INHERITANCE IN C++
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();
}
a
a
b
c
=
=
=
=
5
5
10
50
a = 5
b = 20
c = 100
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:
single inheritance
public inheritance
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
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++ / PRIVATE AND PUBLIC INHERITANCE IN C++ /
SINGLE INHERITANCE IN C++
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.
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:
single inheritance
inheritance in C++
private inheritance
30 JAN, 2014
29 JAN, 2014
LEAVE A REPLY
Name *
Email *
Website
= one
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++ / PROTECTED INHERITANCE IN C++
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:
...
}
Tags:
inheritance
inheritance in C++
//protected derivation
protected inheritance
single inheritance
1 FEB, 2014
29 JAN, 2014
1 RESPONSE
Comments 1
Pingbacks 0
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
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++ / MULTI-LEVEL INHERITANCE
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
};
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();
};
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:
inheritance in C++
types of inheritance
multilevel inheritance
29 JAN, 2014
1 FEB, 2014
3 RESPONSES
Comments 2
Pingbacks 1
||
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
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
PREVIOUS STORY
Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
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.
CONCEPTS OF INHERITANCE, POINTERS, POLYMORPHISM AND VIRTUAL FUNCTIONS IN C++ / INHERITANCE IN C++ / MULTIPLE INHERITANCE IN C++
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.
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:
inheritance in C++
multiple inheritance
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
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
Accessing of the Pointer Variables
void Pointers in C++
Pointers to Functions
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.
POINTERS IN C++
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.
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
Tags:
c++ pointer
c++ pointers
pointer c++
pointers
pointers c
27 FEB, 2014
8 MAR, 2014
LEAVE A REPLY
Name *
Email *
Website
+ 7 = nine
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
POINTERS IN C++
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
declaration of pointer
pointer variables
declaration of pointers
pointers declaration
pointer declaration
3 JUL, 2014
20 FEB, 2014
LEAVE A REPLY
Name *
Email *
Website
4 seven =
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
POINTERS IN C++
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
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;
*p
Similarly:
C++
1 *p = *p + 10
is same as:
C++
1 x = x + 10;
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()
}
Pointer to Object
class pointer
pointer variable
3 JUL, 2014
27 FEB, 2014
LEAVE A REPLY
Name *
Website
Email *
6 nine =
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
PREVIOUS STORY
Introduction to C++
Comment Statements
Identifiers and Keywords
Data Types
Variables and Declarations
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.
POINTERS IN C++
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:
float
int
as:
C++
1 float y;
2 int *p;
3 p = &y;
//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;
void
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 pointers
8 MAR, 2014
23 FEB, 2014
LEAVE A REPLY
Name *
Email *
Website
+ 2 = eight
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
POINTERS IN C++
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);
void
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);
#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);
declares a pointer
The statement:
C++
1 p = &add;
add
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);
Tags:
func to pointer
pointers to function
func to pointers
float
function pointers
float
20 FEB, 2014
3 JUL, 2014
2 RESPONSES
Comments 2
Pingbacks 0
LEAVE A REPLY
Name *
Email *
Website
seven = 2
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.
POINTERS IN C++
this
this
this
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
1 a = 123;
explicitly so far.
Tags:
this pointer
20 FEB, 2014
27 FEB, 2014
1 RESPONSE
Comments 1
Pingbacks 0
LEAVE A REPLY
Name *
Email *
Website
four
= 24
Comment
Post Comment
FOLLOW:
CATEGORIES
Arrays in C++
Expressions in C++
Functions in C++
Inheritance in C++
Multi-Level Inheritance
Operators in C++
Pointers in C++
Programs in C++
MORE
NEXT STORY
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
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.