C++ Summarized Notes
C++ Summarized Notes
C++ Summarized Notes
TUTORIALS OF C++
Marks
Marks the
the end
end of the
of the
main function
(program)
(h) Overloading: Adding a new method with the same name in same/derived class but
With different number/types of parameters. It implements Polymorphism.
Write the merits and demerits of object-oriented language as compared to procedure-oriented
language.
Ans: We can compare the procedure-oriented programming(c) with the object-oriented (c++) .
pop focus more on function
oop focus on data
oop deals with real world object
In pop error detection is difficult as we can’t know which
Variable is associated with which function
In oop we can specify with the object that which variable is
Associated with which function
objects in oop creates many modules of program which is
Flexible and easier to execute and also understand
OOP provides inheritance in which features can be added to
Existing classes without modification
a]In pop importance is given for doing things. In oop importance is given on data rather than procedure.
b]Pop, most of function share global data. oop,data structure are designed such that the characteristics the
object function that operate on the data of an object which are tied together in the data structure
c] Pop, larger programs are divided into smaller programs known as function.oop,program are divided into
smaller programs known as objects.
d]in pop security is not provided for object. In oop security is provided to data.
e]in pop top down approach. In oop bottom up approach.
Tokens - The smallest individual units in a program are known as tokens, c++ has the following
tokens:
Example of tokens:- } ,{, “ “, int
Keywords
Identifiers
Constants
Identifier:
In our everyday, we give names to different things so they can be
Referred easily. Similarly, in C+, we use identifiers to name user created entities
Which may be?
• Variable
• Function
• Type e.g. a class
Every thing has some restrictions and exceptions along with many permissible things. So, does C++ by putting some
restrictions on how we can name these entities. Let us see these rules in details:
1. An identifier can be combination of letters, numbers, and underscores with following restrictions:
a) It should start with a letter or underscore. E.g. height, my_height,_myHeight are allowed but not 1isGod
b) If it starts with a underscore then the first letter should not be capital because such names are reserved for
implementation. E.g. _Height not allowed
2. It should be unique in a program taking care that C++ is case sensitive. E.g. age and Age are different variables
4. There is no restriction on length of the identifier. E.g. h and h_represents_my height are both valid.
Besides restrictions, there are certain guidelines which you should follow:
a.) Use meaningful descriptive names. E.g. int Age is better than int a.
- If description makes identifier name too long then put a comment before identifier and make identifier shorter
b). Be consistent in your naming convention.
- Use small letters for single word identifier name.
- For multiword identifiers, either use underscore separated or intercepted notation. E.g. get_my_height () or
getMyHeight ()
c.) Use Hungarian notation. E.g. double dFlowRate, int value, bool check.
d.) Don't use similar names in a program like Speed, speed, and Speedy
e.) Don't use capitalized version of a keyword like Return
Keywords:
Keywords are predefined reserved identifiers that have special meanings. They cannot be used as
identifiers in your program.
Keyword is a word that the compiler already knows, i.e. when the compiler sees a keyword
somewhere in the program it knows what to do automatically.
For example, when the compiler encounters the keyword ‘int’, it knows that ‘int’ stands for an
integer. Or if the compiler reads a ‘break’, then it knows that it should break out of the current loop.
Some common keywords are-
Constant :
As the name suggests, a variable is something whose value can be changed throughout the program.
It is not fixed. On the other hand, a constant is one whose value remains the same (constant)
throughout the program.
Variable: A variable is the storage location in memory that is stored by its value. A variable is id
denoted by a variable name. The variable name is a sequence of one or more letters, digits or und
Variable decleration
declaration : int a;
declaration means here a is declared as integer variable
Operators: operators play a great role in any languages, the operations are represented by operators
and the objects of the operation are referred to as operands. There are four types of operators.
• Arithmetic
• Relational
• Logical
• Bitwise
In addition, there are some special operators for special tasks.
Operator can be unary (involve 1 operand) , binary(involve 2 operands),and
ternary(involve 3 operands).
Arithmetic operator
In any language, there are some operators to perform arithmetic, logical and control operations.The
basic operators which are used to perform arithmetic operations on integers are as follows:-
Operator Operation
* Multiplication
/ Division
% Modulus (remainder after division)
++ Increment
-- Decrement
The operators +,-,* and / perform the same operation as they do in other languages. The operators +
and – are unary operators and can take one or two operands. The multiply and divide are called binary
operators as they take two operands.
Integer division will always give the result which is of integer type and truncates the remainder. The
output of integer division will be the quotient. For example, 5/2 will give the answer 2 and not 2.5.
The modulus operator gives the remainder after the integer division. For example, 5/2 will give the
answer 1, which is the remainder.
Example- # include<iostream.h>
#include<conio.h>
Void main( )
{
Clrscr ( );
int a=20,b=30,d,e,f,g;
d=a+b;
e=a-b;
f=a*b;
g=a%b;
cout<<d<<endl;
cout<<e<<endl;
cout<<f<<endl;
cout<<g<<endl;
getch( );
}
Result/output:
d=50, e=-10, f=600, g=0.
Relational operator: The relational operator, refer to the relationship that value can have
with one another, it use the Boolean values contains true or false.0 means false and 1 means
true.
Operator Operation
Logical operator :
Operator Operation
&& And
|| OR
! NOT
Table for && (And operator): The operator && corresponds with Boolean logical
operation AND. This operator returns the value of true if both its operands are
true or if it returns false. The following table reflects the value of && operator
p q P&& p q P&&q
q
F F F
0 0 0 F T F
0 1 0 T F F
1 0 0 T T T
1 1 1
Table for || (OR operator): The operator || corresponds with Boolean logical operation OR. The
operator produces a true value if either one of its two operands are true and produces a false value
only when both operands are false. The following table reflects the value of || operator:
p q P&& p q P&&q
q
T T T
1 1 1 T F F
1 0 0 F T F
0 1 0 F F F
0 0 0
Bitwise operator : These operators are used to perform bitwise operations,these operations
are performed on the bits of the binary pattern of the number.bitwise operators refer to
testing,setting or shifting the actual bits in a byte or word, which correspond to the char and
int data types.you cannot use bitwise operator on float,double,long double,void and other
complex operators.
Operator Operation
& And
| OR
~ NOT
^ XOR
>> shift right
<< shift left
p q P&&
q
0 0 0
1 0 1
1 1 0
0 1 1
Conditional Operator
The conditional operator evaluates an expression returning a value if that expression is true and a different value if the
expression is evaluated as false. The syntax is:
For example: In
7>5 ? x : y
Since 7 is greater than 5, true is returned and hence the value x is returned.
Comma Operator
This is denoted by, and it is used to separate two or more expressions.For example:
sizeof() Operator
This operator accepts a single parameter and this can be a variable or data type. This operator returns the size in bytes
of the variable or data type.
For example:
x = sizeof (char);
This returns the size of char in bytes. In this example, the size is 1 byte which is assigned to variable x.
Char:
This keyword is used to declare characters. The size of each character is 8 bits. i.e., 1 byte. The characters that can be used
with this data type are ASCII characters. Its range is -128 to 128.character values (a, b, c, d).
float:
This keyword float is used to declare floating point decimal numbers. The size of each float is 4 byte. its range is -3.4E to 3.4E.
Float value(2.4,3.6,6.5).
long:
This long keyword is used for declaring longer numbers. i.e., numbers of length 32 bits.
Control structure:
Conditional Looping Breaking
Syntax
if
( <expression> )
Statement
Semantics
Example: #include<iostream.h>
#include<conio.h>
Void main()
{
Int a ;
Cout<<”enter the no”;
Cin>>a;
If(n%2==0)
Cout<<”it is even no.”;
getch();
}
If –else: In this statement ,if the expression evaluated to true,the statement
or the body of if statement is executed,otherwise the body of if statement is
skipped and the body of else statement is executed.
if (condition)
{
statement1;
else
{
statement2; }
Example: # include<iostream.h>
#include<conio.h>
Void main()
{
Clrscr();
Int n;
Cout<<”enter the no”;
Cin>>n;
If (n%2==0)
Cout<<”it is even no”;
else
Cout<<”it is odd no”;
getch();
}
Syntax-
switch(expression)
{
Case :exp 1
First case body;
break;
Case :exp2
Second case of body:
break;
Default:
Default case body;
}
Example:- #include<iosttream.h>
#include<conio.h>
int a ;
Cout<<”enter the no”;
Cin>>a;
Switch(a)
{
Case1:cout<<”Sunday\n”;
Break;
Case1:cout<<”Sunday\n”;
break;
Case2:cout<<”monday\n”;
break;
Case3:cout<<”tuesday\n”;
break;
Case4:cout<<”wednesday\n”;
break;
Case5:cout<<”thrusday\n”;
break;
Case6:cout<<”friday\n”;
break;
Case7:cout<<”Satday\n”;
break;
Default:
Cout<<”wrong option”;
}
getch();
}
For loop- In this, first the expression or the variable is initialized and then
condition is checked. if the condition is false ,the loop terminates
Syntax-
for (initillization;condition;increment)
#include<iostream.h>
# include<conio.h>
Void main ()
{
int i;
for(i=1;i<=100;i++)
Cout<<i<<”\n”;
getch ();
}
While loop: This loop is an entry controlled loop and is used when the
number of iteration to be performed are known in advance. The statement in
the loop is executed if the test condition is true and execution continues as
long as it remains true.
Syntax- initialization;
While (condition)
{
Statement;
increment;
}
#include<iostream.h>
#include<conio.h>
Void main ()
{
Int n ,i;
Cout<<”enter the no whose table is to be printed “;
Cin>>n;
i=1;
While (i<=10)
{
Cout<<n<<”x”<<i<<”=”<<nxi<<”\n”;
i++;
}
getch();
}
Do-while - It is bottom controlled loop. This that a do-while loop always
execute at least once.
Syntax- initillization
Do
{
Statement ;
Increement;
}
While(condition);
# include<iostream.h>
#include<conio.h>
Void main()
{
int n;
Cout<<”enter the no. whose table is to be printed”;
Cin>>n;
I=1;
do
{
Cout<<n<<”x”<<i<<”=”<<nxi<<”\n”;
getch();
}
Break statement- The term break means breaking out of a block of code.
The break statement has two use,you can use it to terminate a case in the
switch statement, and you can also use it to force immediate termunation of
loop,bypassing the normal loop condition test.
Example:- #include<iosttream.h>
#include<conio.h>
int a ;
Cout<<”enter the no”;
Cin>>a;
Switch(a)
{
Case1:cout<<”Sunday\n”;
Break;
Case1:cout<<”Sunday\n”;
break;
Case2:cout<<”monday\n”;
break;
Case3:cout<<”tuesday\n”;
break;
Case4:cout<<”wednesday\n”;
break;
Case5:cout<<”thrusday\n”;
break;
Case6:cout<<”friday\n”;
break;
Case7:cout<<”Satday\n”;
break;
Default:
Cout<<”wrong option”;
}
getch();
}
Exit statement- Exit is a function defined in the stdlib library means (stdlib.h).
The purpose of exit is to terminate the current program with a specific exit code. Its prototype is:
exit (exitcode);
The exitcode is used by some operating systems and may be used by calling programs. By
convention, an exit code of 0 means that the program finished normally and any other value
means that some error or unexpected results happened
Example #include<iuostream.h>
#include<conio.h>
#include<stdlib.h>
Void main ()
{
Int n;
Cout<<”enter the no”;
Cin>>n;
If (n%2==0)
{
Cout<<”it is even no”;
else
Cout<<”it is odd no”;
exit(O);
}
getch();
}
Continue:- The continue statement causes the program to skip the rest of the loop in the current
iteration as if the end of the statement block had been reached, causing it to jump to the start of
the following iteration. For example, we are going to skip the number 5 in our countdown:
Goto statement: allows to make an absolute jump to another point in the program. You should use this
feature with caution since its execution causes an unconditional jump ignoring any type of nesting limitations.
The destination point is identified by a label, which is then used as an argument for the goto statement. A label is made
of a valid identifier followed by a colon (:).
Generally speaking, this instruction has no concrete use in structured or object oriented programming aside from those
that low-level programming fans may find for it. For example, here is our countdown loop using goto:
#include <iostream>
using namespace std;
int main ()
{
int n=10;
loop:
cout << n << ", ";
n--;
if (n>0) goto loop;
cout << "FIRE!\n";
return 0;
}
What is array and also explain the different types of arrays?
Array :- It is a collection of similar type of data which may be int type, char type, float type or
user-defined type such as structure or class. The significance of an
array is that each array element is stored in consecutive memory locations and the array elements are
accessed by their index value, which is also called subscript value.
General format of array:
data type array name[size];
#include<iostream.h>
#include<conio.h>
Void main()
{
Clrscr();
Int a[10],i;
for(i=1;i<=10;i++)
{
Cout<<”enter the no:”;
Cin>>a[i];
}
for(i=1;i<=10;i++)
{
Cout<<a[i]<<endl;
}
getch();
}
write a program which calculate how much spent in total ?
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int choc [4];
cout<<”enter the no of chocolates”;
for (int i= 0;i<4;i++)
cin>>choc[i];
int total =0;
for (i=0;i<4;i++)
total =total +(choc[i] x 5);
cout<<”total cost of chochalate is :”<<total ;
getch();
}
#include<iostream.h>
#include<iostream.h>
void main()
{
int A[3][4],B[3][4],C[3][4];
int r,c;
// read value in matrices
Cout<<”enter the first matrix row wise \n”;
for (r=0;r<3;r++)
{
for(c=0;c<4;c++)
{
Cin>>A[r][c];
}
}
Cout<<”enter the second matrix row wise\n”;
for (r=0;r<3;r++)
{
for (c=0;c<4;c++)
{
Cin>>B[r][c];
}
}
//addition of two matrix
for (r=0;r<3;r++)
{
for (c=0;c<4;c++)
{
C[r][c]= A[r][c]+B[r][c];
}
}
//display the matrix
for (r=0;r<3;r++)
{
for (c=0;c<4;c++)
{
Cout<<C[r][c]<<”\t”;
}
Cout<<”\n”;
}
}
getch();
}
What is Function?
• Library function
In c++ three terms are always associated with the function are:
Function definition
Normally, a function call transfers the control from the calling program to the function and after the execution of
the program returns the control back to the calling program after the function call. These concepts of function
saved program space and memory space are used because the function is stored only in one place and is only
executed when it is called. This concept of function execution may be time consuming since the registers and
other processes must be saved before the function gets called. The extra time needed and the process of saving is
valid for larger functions. If the function is short, the programmer may wish to place the code of the function in
the calling program in order for it to be executed. This type of function is best handled by the inline function. In
this situation, the programmer may be wondering “why not write the short code repeatedly inside the program
wherever needed instead of going for inline function?” Although this could accomplish the task, the problem lies
in the loss of clarity of the program. If the programmer repeats the same code many times, there will be a loss of
clarity in the program. The alternative approach is to allow inline functions to achieve the same purpose, with the
concept of functions.
What are virtual functions? Describe a circumstance in which virtual functions would be appropriate
Virtual function
Virtual functions are functions with the same function prototype that are defined throughout a
class hierarchy. At least the base class occurrence of the function is preceded by the keyword
virtual. Virtual functions are used to enable generic processing of an entire class hierarchy of
objects through a base class pointer. For example, in a shape hierarchy, all shapes can be drawn.
If all shapes are derived from a base class Shape which contains a virtual draw function, then
generic processing of the hierarchy can be performed by calling every shape’s draw generically
through a base class Shape pointer.
What is a return statement in function and how many return statements can be used
In a function?
Ans: Return statement is used to return the value from the function definition to the
function call in the program. Only one return statement is executed in the function.
It contains data members and member function which are declared under
class.
Public: In this data members and member function are accessible outside
the class.
Private: Data members and member function are not accessible outside the
class.
#include<iostream.h>
#include<conio.h>
Class add
{
Public:
int a,b,c;
Void input ();
Void output ();
};
Structure program:
To print the student name, branch, roll no and age?
# include<iostream.h>
#include<conio.h>
Struct student
{
Char name[10];
Char branch[10];
Int rollno;
Int age;
};
Void main( )
{
Student s;
Cout<<”\n enter the name of student”;
Cin>>s.name;
Cout<<”\n enter the branch of student”;
Cin>>s.branch;
Cout<<”\n enter the age”;
Cin>>s.age;
getch();
}
Inheritance: Inheritance is the process by which new classes called derived
class are created from existing class called base class. The derived class has
all the features of base class and the programmer can choose to add new
features specific to the newly created derived class.
For example: a programmer can creat a base class “fruit” and derived class
as “mango”, “apple”, “banana”,”orange”.
Types of inheritance:
B
Derived class
A
A Base class
B
B Derived class
C
C Derived class
A B
When many subclasses inherit properties from a single base class, it is called as
hierarchical inheritance. The base class contains the features that are common to
the subclass and a subclass can inherit all or some of the features from the base
class as shown below
AA
B C
B D