BASIC CONCEPT OF C++
C++ is an object-oriented programming language through which we can
communicate with another computer system.
It is developed by B. Jarane Stroustrup at AT&T Bell Laboratories in The USA in 1980.
Its initial name was ‘C with
Classes’ and after that rename as c++ in 1983.
It is a superset of the C language.
It is an extended or incremented form of C language.
It is a case-sensitive language. i.e it treats different in uppercase
& lowercase.
It is a third-generation language.
It is a high-level language.
It is a platform-independent language. i.e it supports any operating system like Windows XP, Windows 10... etc.
It is used in both writings in system software and application software.
OBJECT ORIENTED PROGRAMMING:- it is a way of programming that allows modularizing a program by forming separate memory area for data as well as function.
CHARACTERISTICS OF OBJECT-ORIENTATION PROGRAMMING:-
- Programs are divided into classes and their number of functions.
- New data items and functions can be easily added whenever essential.
- Data is private and prevented from accessing external functions.
- An object-oriented program is more focused on data than function.
- An object can communicate with each other through functions.
- O.O.P can be easily upgraded without affecting the other parts of the program.
- Redundant program code can be eliminated using inheritance.
- O.O.P allows polymorphism, encapsulation, and other characteristics.
- O.O.P enhances the process of programmers to the rapid development of new software in a short time.
Uses of O.O.P
- To develop Object-oriented DBMS application software.
- To develop system software.
- It is used to develop office automation software.
- It is also used to develop A.I(artificial intelligence) and expert system.
- It is used in CAD(computer-aided design)
- It is used in CAM(computer-aided manufacturing).
There are following O.O.P language such as-
Ø Simular67
Ø C++
Ø Charm++
Ø Java
Ø C# .......etc
KEY CONCEPT
OF O.O.P
CLASS:-
- it is a user-defined data type.
- Class is a group of similar objects
that can share their common properties and relationship.
- A class serves as a blueprint or
template for its objects that are any number of objects related to the class can
be created.
- A class represents the data and
functions.
- Example – ‘fruits’ is a class and its object is banana, apple .etc.
Object:- An object is a runtime entity like a chair, pencil...etc.
Ø Object is like a variable of type
class.
Ø An object consists of the state and the behaviour of related class and function.
class is a blueprint for objects. and class is also an object factory.
Method / Function:- The operation required for an object when coded in a class is called Method.
it is a set of instruction that are developed for a specific purpose to perform a particular task
Data
Abstraction:- The
representation of essential features without including the background details is
called data abstraction.
Ø Abstraction is a mechanism or the technique to hide irrelevant(unwanted) details and representation of essential
features so that one can focus on important things at a time.
Ø Example:- while driving, A driver only knew the essential features
such as how to use the clutch, brake, accelerator, gears...etc.
Polymorphism
:- it is a Greek word that means multiple forms.
Ø Polymorphism allows the same function
to act differently.
Ø Example- function overloading,
operator overloading ..etc.
Encapsulation
:- The packing of
data and function into a single component are known as encapsulation.
Inheritance:- it is the method or technique by
which objects of one class get the properties of the object of another class.
Ø Inheritance allows us to derive a new
class from an existing class.
Structure of the C++ program
file section
|
Class class-name { Private: Data member Member function
Or
Public: Data member Member function
Or Protected: Data member Member function
}; |
Void
main() { ..................... ..................... ..................... Statement } |
FThere
are following types of tokens such as:-
- Keyword
- Identifier/variable
- Constant/literal
- Operator
- Special character/ separator/ punctuator
Keyword :- A keywords have a fixed meaning and these meanings can not be changed. There are 63 keywords in c++, keywords are reserve words for the system. So, these are also known as reserve words.
- Keywords are the predefined words that have special significance and importance.
- Keyword can not be used to declare a variable name, class name, function name, and object name.
F There
are following keyword listed below –
char |
short |
int |
long |
enum |
true |
false |
double |
float |
void |
struct |
union |
typedef |
signed |
unsigned |
volatile |
register |
const |
Static |
extern |
auto |
static cast |
dynamic-cast |
const - cast |
if |
else |
switch |
case |
default |
for |
while |
do |
break |
continue |
return |
goto |
new |
delete |
class |
friend |
private |
public |
protected |
template |
virtual |
operator |
this |
try |
throw |
catch |
using |
namespace |
sizeof |
inline |
wchar-t |
reinterpret-cast |
type id |
type
name |
mutable |
explicit |
boolean |
export |
|
|
| Identifiers:- the meaningful variable name, the function name is known as identifiers.
F An identifier
is a user-defined word.
F Example
– length, sum, area, height.
| Variable
:- it is a data name that holds data value.
F It allocates
the memory location to the value.
F It is
like a container of data.
F There
are following rules for defining variable name or identifier-
Variable
name must begin with a character without space but underscore is permitted.
The variable the name should not start with a digit.
The variable name may be a combination of uppercase and lowercase characters.
Variable
should not be a c++ keyword.
The length of the variable name may up to 31-character.
Visibility
mode/ access specifier:- it determines which data where to access.
There are three
access specifiers or visibility mode in c++ such as –
1. Private
2. Public
3. Protected
1. Private:- Data member and member function declared under this section can not be accessible outside of the class.
Ø If there are no access specifiers declared then by default takes private access specifier.
2. Public:- Data member and member functions declared under this section can be accessed outside of the class.
3. Protected:- Data member and member functions declared under this section can be accessed outside of the class in inheritance.
NOTE:- Data member:- A variables declared inside the class is called data
member or member variable.
Member function:- A function declared inside the class is called member function.
Constant:- constant refers to fixed value and value cannot be changed
during the program execution.
Ø It is also known as literals.
Ø We use the ‘const’ keyword for declaring
constant.
Ø We can also use the #define for
declaring constant.
Ø E.g. #define
a 10
Ø There are two types of constant such
as –
1. Numeric
2. Character
1. Numeric constant:- it is a negative or positive numeric value that
is either an integer or fractional constant.
2. Character constant:- it is one or
more characters that are enclosed within single quotes.
e.g:- ‘A’ ‘B’......etc
Ø String constant also belongs to
this character constant is always written within double quotes.
Ø E.g – “vmsm”
3. Operator:- An operator is a symbol that performs particular
operation between the operands( like addition, subtraction, division,
multiplication etc.)
E.g- S= A+B, here both A, B are the operands and the ‘+’ sign is an
operator.
Ø There are three types of the operator
such as-
1. Unary
operator
2. Binary operator
3. Ternary operator
| Unary operator:- An operator which
are required one operand to perform a particular task.
Ø There are two unary operators such as-
1. Incremented operator ( ++)
2. decremented operator (--)
E.g- A++
, A-- , ++B , --B.
Increment
& decrement operator:- -
Ø This operator works as a shorthand tool
to increase or decrease the value of the variable by one.
Ø “++” increase the value of a variable
by one. i.e. i++= i+1.
Ø “-
-“ decrease the value of a variable by one. i.e. i-- = i-1.
Ø These operators can be used in two
ways such as-
1. Pre increment or post-increment
2. Pre decrement or post decrement
1. Pre increment or pre decrement:- when the operator is placed before operands
then this is known as pre-increment or pre decrement operator.
E.g- ++a , --a
Ø It process first then incremented or the decremented value will be assigned to any term.
2. Post increment or post decrement:-
when the operator is placed after operands then this is known as post-increment
or post decrement.
E.g.- a++ , a--.
Ø It receives value as it is then incremented or decremented value will be share to any term.
Q. write a program in c++ by using increment or decrement operator .
#include<iostream.h>
#include<conio.h>
void mian()
{
clrscr();
int n=10, S,T;
S= n++;
T=++n;
cout<<”S=”<<S<<”\n”;
cout<<”T=”<<T;
getch();
}
Output
10
11
| Binary operator:- those operators which are required two operands to
perform any task.
e.g.- A+B, where A, B both are
operands and + sign is an operator.
Ø There are following binary operator
such as-
+,-,* , / , %.....etc.
Q. write a program to find addition, subtraction, and multiplication, and division.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int A=10, B=20, sum, sub, mult;
sum=A+B;
cout<<"sum=";
cout<<sum;
sub=B-A;
cout<<"\n subtraction=";
cout<<sub;
mult=A*B;
cout<<"\n multiply=";
cout<<mult;
getch();
}
output
sum=30
subtraction=10
multiply=200
| Ternary operator:- those operators which required more than two operands are known as the ternary operator.
e.g- if(n1==n2)? True : false
It is also known as conditional
operator.
Example :-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x =100, z;
z=(x==100
&& x==1000?1 : 0);
cout<<z;
getch();
}
Output
1
TYPES OF OPERATOR
There are following types of operators such as-
1. Arithmetic operator
2. Relational operator
3. Logical operator
4. Assignment operator
5. Bitwise operator
6. Conditional operator
7. Increment decrement operator
8. Special operator
1. Arithmetic operator:- it performs the
basic arithmetic operations on operands.
Ø It requires a numeric value.
Ø E.g.- + , - , * , %, /
2. Relational operator:- these operators are used to distinguish
between two values depending on their relations.
Ø It provides the relationship between
two expressions.
Ø These are also known as
comparison operator.
Ø These are used generally create
conditions.
Ø E.g.- < , > , <= , >= , == , !=
3. Logical operator:- it is used to
testing logical conditions between operands.
Ø These are used to combined or negate
expressions. After checking conditions,
it provides logical true (1) or false
(0) status.
Ø E.g.- 1. logical AND(&&)
2.Logical
OR(||)
3. logical NOT(!)
Logical AND
(&&):- These operators provide true results when both expressions are true(1) otherwise false(0).
E.g.-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x =100, z;
z=(x==100
&& x==1000?1 : 0);
cout<<z;
getch();
}
Output
1
Logical
OR(||):- These operators provide
true result when one any
expressions are true(1) otherwise
false(0).
e.g.-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x =100, z;
z=(x==100
||x==1000?1 : 0);
cout<<z;
getch();
}
Output
1
Logical NOT
(!):- these operator provides true result if input is zero(0) otherwise gives
one(1).
#include<iostrteam.h>
#include<conio.h>
void main()
{
clrscr();
int x=100,
z;
z=(x!=100?
1:0);
cout<<z;
getch();
}
Output
0
Assignment
operator:- it is used to assigning the value to the variable.
e.g.- a=5
there are
some assignment operator such as
= , <= ,
>= , /=, %= , +=, -=, <<= , >>=, >>>= , &= , !=, ^=
#include<iostrteam.h>
#include<conio.h>
void main()
{
clrscr();
int x=10, z;
if(x>=10)
{
cout<<” true”;
}
else
{
cout<<” false”;
}
getch();
}
Output
true
Bitwise
operator:- this operator is used to a bit
manipulation and can only be applied to integer operands.
Ø There are following bitwise operator
such as-
& - bitwise AND
| - bitwise OR
^ - bitwise XOR
~ - bitwise NOT
>> - right shift
<< - left shift
Bitwise
AND:- it returns 1. if both the bits are 1 and it returns zero if one any the bit will be zero.
E.g.-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b,c ;
a=15;
b=9;
c=a&b;
cout<<”c=”<<c;
getch();
}
Output
9
Bitwise
OR(|):- it returns 1 if anyone bit is 1. otherwise it returns zero if both
bits are zero.
E.g.-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=15,
b=9,c;
c=a|b;
cout<<”c=”<<c;
getch();
}
Output
15
Bitwise NOT
(~):- it is also known as the complement operator. It returns zero if the input bit is
one. otherwise, it returns one if the input
bit is zero.
include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=15 , c;
c=~a;
cout<<”c=”<<c;
getch();
}
Output
-16
Bitwise
XOR:- it returns zero if both bits are the same. Otherwise returns one.
E.g.-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
a=15;
b=9;
c=a^b;
cout<<”c=”<<c;
getch();
}
Output
6
Commas operator:- it is used to separating variables and expressions.
Conditional
operator:- it is used to checking a condition and execute any single expression
according to it.
Ø There are three parts of this operator such as-
<condition >? <true block>: <false block>
#include<iostream.h>
#include<conio.h>
void main()
{
int n;
clrscr();
cout<<”enter
any number n=”;
cin>>n;
n%2==0?cout<<”even
number” : cout<<”odd number” ;
getch();
}
Output
Enter any
number=4
even number
v Statement - instruction is written in a high-level language or defined a task in a program is called a statement.
There are two types of statement such
as-
1. Single line statement
2. Compound statement
1. Single line statement:- it determines a single task. And it is terminated with a semicolon.
2. Compound statement:- it determines multiple tasks. It is always enclosed within curly braces.
Conditional statement:- The statement which is used to create a condition of making a decision in program is known as conditional statement.
If statement
:- it is a conditional statement and used for decision making. it is used in
four ways such as-
1. 1.Simple if
2. 2.if else
3. 3. Nested if-else
4. 4.Ladders if else
Simple if:- it is a conditional statement. it is used to
execute a set of statements if the condition is true.
Ø Multiple conditions can be combined
with logical operators.
Ø It is also known as a single structure.
Syntax:-
if( test
condition)
{
Statement;
}
Next
statement;
Flow chart:-
Q write a
program to check person is eligible for vote or not.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int age;
cout<<”Enter age of person=”;
cin>>age;
if(age>=18)
{
cout<<”eligible
for vote=\n”;
}
cout<<” thanks
“;
getch();
}
Output
Enter the age of
person=19
Eligible for
vote
thanks
if-else statement:-it is a conditional statement. it is used to express or execute a different set of statements according to the true and false value of a given
condition.
Ø It is similar to conditional statement.
Syntax :- if(test condition)
{
True block statement;
}
else
{
False block statement;
}
Flow chart:-
Q. write a
program to print number is even or odd.
#include<iostream.h>
#include<conio.h>
void main()
{
int n;
cout<<”Enter
any number=”;
cin>>n;
if(n%2==0)
{
cout<<“number
is even”;
}
else
{
cout<<” number
is odd”;
}
getch();
}
Output
Enter any
number= 4
number is
even.
Q. write a
program in C to check number is prime or not.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n
, i, s=0 , f=0;
cout<<”enter
any number”;
cin>>n;
s=n/2;
for(i=2;
i<=s; i++)
{
if(n%i==0)
{
cout<<”number
is not prime”;
f=1;
break;
}
}
if(f==0)
{
cout<<” number
is prime”;
}
}
Enter any
number=4
Number is
not prime
Nested if
else:- when a series of decision are involved
then we have to use more than one if-else statement in a nested form.
Ø One if-else within another if-else is
known as nested if-else
Ø ‘C++’ allows 256 levels of nested if
else in a program.
Syntax :-
if(test condition)
{
if(test
condition )
{
Statement ;
}
}
else{
Statement ;
}
else{
if( test condition)
{
Statement;
}
else
{
Statement;
}
}
Ladder if
else:- it is a conditional statement and used for decision forming when we
have multiple options or cases.
Ø In this statement each ‘if’ contains
an expression or evaluated from top to bottom.
Ø When a condition found true the
statement associated with ‘if’ is executed and control exit from the condition.
Ø If none of the conditions is
satisfied then else block is executed.
Ø Syntax:-
if(test condition)
{
Statement;
}
else if(test condition)
{
Statement;
}
else if (test condition)
{
Statement;
}
else if(test condition)
{
Statement;
}
else
{
Default statement;
}
Flow chart :-
Ø It is used for iterative or
repetitive task.
Ø It requires a label followed by
colon(:) symbol.
Ø Syntax :- goto label name;
Ø E.g.- goto vmsm;
Q. write a
program to print your name five times.
#include<iostream.h>
#include<
conio.h>
void main()
{
clrscr();
int i;
i=1;
vmsm:
cout<<”\n
vikas mishra”;
i++;
if(i<=5)
goto vmsm;
getch();
}
OUTPUT
Vikas mishra
Vikas mishra
Vikas mishra
Vikas mishra
Vikas mishra
Q. write a
program in c++ by using goto statement
to print a table of the input number.
Q. write a
program to find factorial of input number by using goto statement.
LOOP:- it
allows execution of statements of a partition of a program repeatedly a given
number of times or a certain condition is satisfied.
Ø When the condition becomes false then the loop will be end and control goes out from the control loop.
Ø It is used for executing
repeatedly of any statement.
Ø There are two types of loop such as-
1. Entry control loop.
2. Exit control loop.
1. Entry control loop:- in this loop
condition is checked first then execute the statement body of the loop.
Ø There are two entry control loop such
as-
1. for loop
2. while loop
Exit control loop:- in this loop execution
of statement is first then check the
condition.
Ø There is one exit control loop –
1. Do- while loop.
for loop:-
this lop is the easiest loop among all the loop in ‘c++’.
Ø It contains all the loop control
statements in one place in a pair of parenthesis.
Ø Which make it more clear and
understandable.
Ø This loop is used for fixing
iteration.
Ø This loop comes in the category of the entry control loop and unconditional loop.
Ø It contains initialization, test condition and increment
/decrement.
Ø The control checks the initialization first
and then check the condition and executes the body of the loop if the test condition is
satisfied.
Ø Again control goes to increment/
decrement and then again check the condition.
Ø If the test condition is satisfied then again the body of the loop is executed this
process continuously until the test condition
becomes false.
Ø If the test condition is not satisfied then control exits from the loop.
Syntax :- for(initialization ;
test condition ; increment/decrement)
{
Statement;
}
Flow chart:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1; i<=5; i++)
{
Cout<<”VMSM programming classes by vikas mishra\n”;
}
getch();
}
OUTPUT
VMSM programming classes by vikas mishra
VMSM programming classes by vikas mishra
VMSM programming classes by vikas mishra
VMSM programming classes by vikas mishra
VMSM programming classes by vikas mishra
#include<iostream.h>
#include<coniio.h>
void main()
{
clrscr();
int n, i, r1=-1 , r2=1, r3;
cout<<”enter any number=”;
cin>>n;
for(i=1;i<=n;i++)
{
r3=r1+r2;
cout<<”\t”<<r3;
r1=r2;
r2=r3;
}
getch();
}
While loop:- this loop comes in the category of entry
control loop or conditional loop.
·
This loop executes its body of
statements repeatedly as long as a specified condition is true.
·
In this loop test condition is evaluated
first, if the test condition is true then the body of the loop will be executed.
·
In this loop after execution again test
condition is evaluated and if found true then the body of the loop is
executed again. this process continues until the condition
becomes false.
·
When the specified condition becomes false
then control is the exit from the loop.
Syntax:- while ( test
condition)
{
Body
of the loop
}
Q. write a program to print your name five times by using while loop.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i=1;
while(i<=5)
{
cout<<”vikas mishra”<<”\n”;
i++;
}
getch();
}
Output
Vikas mishra
Vikas mishra
Vikas mishra
Vikas mishra
Vikas mishra
DO-while loop:- This loop comes in the category of exit control loop and conditional loop.
Ø This loop executes its body of statement at least once whether the specified condition is true or false.
Ø It executes its body of statement first then evaluates the test condition. If the test condition is satisfied then it again executes its body. This process continues until the test condition becomes false.
Ø When the test condition is not satisfied then go-out from the loop and the loop end.
Ø Syntax:-
do
{
Statement;
Increment /decrement;
}
While (test condition);
Flow chart:-
Q. write a program to print your name five times by using do-while loop.
#include<iostreamh>
#include<conio.h>
void main()
{
int i=1;
clrscr();
do
{
cout<<”vikas mishra\n “;
i++;
}
while(i<=5);
getch();
}
OUTPUT
Vikas mishra
Vikas mishra
Vikas mishra
Vikas mishra
Vikas mishra
Nested loop:- one loop within another loop is called nested loop.
Syntax :-
for( initialization; test condition; increment / decrement)
{
for( initialization; test condition; increment/ decrement)
{
Statement;
}
}
Q. write a program to print the following format.
****
****
****
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i , j;
for(i=1;i<=4;
i++)
{
for(
j=1;j<=4;j++)
{
cout<<”*”;
}
cout<<”\n”;
}
getch();
}
OUTPUT
****
****
****
FUNCTION:- The number of statements grouped in a logical unit to perform some specific task is called function.
Ø Function makes the program easier to understand and maintain.
Ø A large
program is broken down into small functions. i.e. A large program is made from variables small
function.
Ø Error
detection and correction are easy when we use the function in a program.
Ø It also provides
code reusability facility.
There are two
types of function such as –
1. Library /built
in / predefined functions
2. User –define function
1. Library function:-predefined function are called library function. We can use library function by including the respective header file.
Ø We can not
understand the internal structure of library function.
Ø We can not
modify of this function.
Ø E.g.- sqrt() , pow(), ...etc.
2. User-define
function :- The function defined by the user is called user defined function.
Ø We can
understand the internal working structure of user define function.
Ø We can modify
user-defined function.
Ø There is no need to include any header file.
Parts of function :-
1. Function
prototype:- it determines the name of function and number of argument and their type and also returns
type.
Ø It is generally located at the top of the program.
2. Function calling:- when we want to execute the statement of the function then we call function. It contains the actual argument.
3. Function
definition:- it contains the various statements which to be executed after
calling.
Ø Generally
it is located outside of the main function.
Ø It contains formal arguments.
4. Actual argument:- Argument written in function calls is called an actual argument.
5. Formal argument :- Arguments written in function definition is known as a formal argument.
6. Return state:- it sends the value to the calling function.
Ø User -define function can be used in three ways –
i. No argument no
return type.
ii. With argument but not return type.
iii.With argument with the return type.
Syntax:-
int
sum(int x, int y )
{
int z=x+y
return z;
}
void main()
{
clrscr();
sum(a,b);
getch();
}
Passing argument in function in
three ways such as-
1. Call by
value /pass by value
2. Call by
address/pass by address
3. Call by reference/pass by reference
| Call by value
/ pass by value:- in this technique, value is passed in the function argument.
Ø In this
technique actual argument is copied to the formal argument and all operations are
performed on the formal argument.
Ø Any change
made an informal argument then does not nay an effect on the actual argument.
E.g.- write a program to represent
call by value/pass by value.
| Call by reference /pass by reference:-in this technique memory address is passed to function argument.
Ø In this technique
address is implicitly passed to formal arguments.
Ø Any changes
made informal arguments affect the actual argument. i.e. changes are made permanent.
Ø E.g.:-
write a program to represent the swapping by using call by reference.
| Call by
address/pass by address:-in this technique memory address is passed to function
arguments.
Ø In this
technique address explicitly passed to the formal arguments.
Ø Any changes
made informal arguments effects in actual arguments. i.e. change is made
permanent.
Ø E.g.- write
a program to represent the swapping by using a call by address.
Function overloading:- it means
that multiple functions in a class having the same name with different arguments
for different purposes.
It is an example of polymorphism.
It is an important feature of object oriented programming.
Example :-
#include<iostream.h>
#include<conio.h>
public class num
{
int r,l,b,h;
void area(int);
void area(int , int);
void area(int ,int,int);
};
void num::area(int r)
{
cout<<”enter value of r=”;
cin>>r;
cout<<”Area of circle=”<<3.14*r*r;
}
void num::area(int l,int b)
{
cout<<”Enter the value of l and b=”;
cin>>l>>b;
cout<<”Area of rectangle=”<<(l*b);
}
void num::area(int l, int b, int h)
{
cout<<”Enter the value of l,b,h=”;
cin>>l>>b>>h;
cout<<”volume of the cuboids=”<<(l*b*h);
}
void main()
{
clrscr();
num obj;
obj1.area();
obj2.area();
obj3.area();
getch();
}
Output
Enter the value of r=2
Area of circle=12.56
Enter the value of l and b=2
3
Area of rectangle=6
Enter value of l,b,h=2
3
4
The volume of cuboids= 24
Friend class:- A class which is declared with friend keyword and private data member of another class is known as a friend class.
It is possible to declare one or more friend class.
Example:-
class vmsm
{
private :
int a, b;
friend class pro;
};
Constructor:- it is a special member function that is used to
initialize data member.
Ø It is different from all other member function because it
has the same name as its class name and automatically invoked by the compiler when the object is created.
Ø It is similar to other member functions. i.e. it can be
defined inside or outside of the class and also be passed parameter or
argument.
There
are following characteristics of constructor such as-
Ø Constructor has the same name as its class name.
Ø A constructor should be declared in the public section.
Ø Constructor is used to initializing the data member of the class.
Ø Constructor does not return any type not even void.
Ø Constructor calls automatically when the object is created.
Ø Constructor can be overloaded i.e. multiple constructors
can be used in a single class.
Ø A constructor is not
declared in the class then the default constructor is generated by the compiler.
Ø Constructor follows all the access rules which are
followed by another member function of class.
Ø It can be parameterized like other member function.
Ø Constructor can not be virtual or cant is inherited.
Types of constructor:
There
are three types of constructor such as-
1.
Default constructor
2.
Parameterized
constructor
3. Copy constructor
1 Default constructor:-
A constructor without a parameter or argument is known as the default constructor.
Syntax
:-
Class class_name
{
public:
class_name()
{
Statement;
}
};
Example
:-
#include<iostream.h>
#include<conio.h>
class
Vmsm
{
private:
int num1 , num2;
public:
Vmsm()
{
num1=20;
num2=40;
}
void
show()
{
cout<<”num1=”<<num1;
cout<<”num2=”<<num2;
}
};
int
main()
{
Vmsm
obj;
obj.show();
return
0;
getch();
}
Output
num1=20
num2=40
Parameterized constructor:- A constructor which is declared with parameter is known as parameterized constructor.
It may be single or multiple.
Syntax:-
Class
class_name
{
public:
int
a,b;
Class_name(int
x, int y)
{
Statement;
}
};
Example
:-
#include<iostream.h>
#include<conio.h>
class
VMSM
{
private:
int
b,c;
public:
VMSM(int
b1, int c1)
{
b=b1;
c=c1;
}
int
getX()
{
return
b;
}
int
getY()
{
return
c;
}
};
int
main()
{
VMSM
ob(30,40);
cout<<”value=”<<
ob.getX()<<”value=”<<ob.getY();
return
0;
getch();
}
Output
Value=30 value=40
Copy constructor:- it is a type of constructor which is used
to copy the value of one object to another object of the some class.
Syntax:
Class class_name
{
Access modifier:
Class _name(const class_name &ob)
{
Statement;
}
};
Example :-
#include<iostream.h>
#include<conio.h>
class vmsm
{
public:
int x;
vmsm(int a)
{
x=a;
}
vmsm(vmsm &ob)
{
x=ob.x;
}
};
int main()
{
vmsm a1(30);
vmsm a2(a1);
cout<<a2.x;
return 0;
getch();
}
Output
30
Constructor overloading:- it means multiple constructors in a
class with different argument for a different purpose.
Example :-
#include<iostream.h>
#include<conio.h>
class vmsm
{
public:
float area;
vmsm()
{
area=0;
}
vmsm(int a, int b)
{
area= a*b;
}
void show()
{
cout<<area;
}
};
int main()
{
vmsm ob;
vmsm obj(10, 30);
ob.show();
obj.show();
return 1;
getch();
}
Output
0
300
Destructor:- A destructor is a special member function of a
class that automatically executed when an object is destroyed.
It has the same
name as its class name but proceeded with ~ symbol.
It is used
to destroy the object that has been created.
It never
takes any argument and does not return value i.e. destructor is used to deallocate
memory space or cleaning the operation or task performed by an object.
It can not
be overloaded.
Destructor
follows all the access rules as are followed by the other member function.
When no
destructor is in the class then compiler invokes automatically default destructor.
Example:-
vmsm
obj;
ob.vmsm::~vmsm();
program :-
#include<iostream.h>
#include<conio.h>
class vmsm
{
private:
int n1, n2;
public ;
vmsm(int x, int y)
{
cout<<”inside constructor”<<”\n”;
n1=x;
n2=y;
}
void show()
{
cout<<”n1=”<<n1<<”\n”;
cout<<”n2=”<<n2<<”\n”;
}
~vmsm()
{
cout<<”inside destructor”;
}
};
int main()
{
vmsm obj1(10,30);
obj1.show();
obj1.vmsm::~vmsm();
getch();
return 0;
}
Output
Inside constructor
n1=10;
n2=30;
inside destructor
Inheritance:- it is
the most important feature of object-oriented programming.
It is the
method or technique by which objects of one class gets the properties of
objects of another class.
It is a way
of creating of a new class from the existing class.
It provides
the facility of code reusability. i.e. redundant code can be removed.
It enables
to derive a new class which is known as derive /child/descendent/subclass from
the existing class which is known as base/parent /ancestor /superclass.
Derive class:-it can access a member of the base class according to the rule of inheritance. But the base class can not access a member of derived class.
Base class:- A class which allows accessing data members and member function to a derived class is called a base class.
Derived class:- A class that access or inherit the properties of the base class is called derive class.
Derive the class automatically access the data member and member function without rewriting them again. A derive class may also be a base class of other derived class.
There are following rules for inheritance such as-
👉base class can be inherited in three ways or mode.
1. public
2. private
3. protected
1. Public
mode:- when base class is inherited
with public mode then the public member of the base class become a public member of
derived class.
2. Protected
mode:- when the base class is inherited with protected mode then the protected
member of base class becomes protected member of derived class.
3. Private
mode:- when the base class is inherited with private mode then the private
members of base class become a private member of the derived class.
Syntax :- class
derived _ class name : mode /access specifier
base_class
{
Stmt ;
};
Example :-
class vmsm
{
public:
int a,b,c;
};
class vm:
public vmsm
{
Statement;
};
Write a program to enter time in
seconds. And then display after converting them into hours, minutes, and
seconds.
This comment has been removed by a blog administrator.
ReplyDelete