Professional Documents
Culture Documents
,RMKMHSS - 601206
Namma Kalvi
11 www.nammakalvi.in
RMKMHSS
COMPUTER SCIENCE
GUIDE
VOLUME II
ELANGOVAN
M.Sc,B.Ed,M.Phill
rmkmhsselango.blogspot.com
9677515019
1
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
UNIT -III
INTRODUCTION TO C++ CHAPTER
9
Introduction to C++
6.Define Keywords.
1.History of C++ Keywords are the reserved words.
It conveys specific meaning to the C++ compiler.
C++ was developed by Bjarne Stroustrup at AT & T
Bell Laboratory during 1979 They are the essential elements to construct C++
programs.
it was referred “New C” and “C with Classes”.
Most of the keywords are common to C, C++ and
In 1983, the name was changed as C++ by Rick
Java.
Mascitti.
2.Benefits of learning C++ All the keywords must be in lower case in C++.
C++ is called as Hybrid Language. keywords cannot be used as an identifier name.
Ex. if ,for ,this, cout , cin, while etc….
C++ is for multi-device, multi-platform app
7.What are Identifiers?
development.
Identifiers are the user-defined names given to
C++ is an object-oriented programming language.
variables, functions, arrays, classes etc.,
It includes classes, inheritance, polymorphism,
These are the fundamental building blocks of a
data abstraction and encapsulation.
program.
C++ has a rich function library.
Every language has specific rules for naming the
C++ allows exception handling, inheritance and
identifiers.
function overloading which are not possible in C.
8.What are the Rules for naming an identifier?
C++ is a powerful, efficient and fast language.
Only alphabets, digits and underscore(_) are
It finds a wide range of applications.
permitted.
3.List the languages which are influenced by C++
Other special characters ,space are not allowed.
C# (C-Sharp), D, Java and newer versions of C
The first character of an identifier must be an
4.What are the Character set in C++?
alphabet or an underscore (_).
Character set is a set of characters which are
• C++ is case sensitive
allowed to write a C++ program. They are
• keywords cannot be used as an identifier name.
9.Define Literals /Constant? What is meant by literals?
Literals are called as Constants.
Literals are data items whose values do not change
during the execution of a program.
C++ has several kinds of literals:
Numeric Constants:
Boolean Literals
Character constant
String constant.
10.Numeric Constants: What are numeric constant and
its types.
The numeric constants are only numeric values,
They are,
5.What are Lexical units or Lexical elements or Tokens. 1. Integer Constants (or) Fixed point constants.
in C++? 2. Real constants (or) Floating point constants.
The smallest individual unit in a program is known
as a Lexical unit or Lexical elements or Token. They
are Keywords • Identifiers • Literals • Operators
• Punctuators
2
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
11.How many types of integer literals(or) Fixed 17.What is the significance of null (\0) character in a
point constants available in C++? or string?
Explain the types of integer literals in C++. String should be terminated with a ‘\0’ (NULL)
Integers are whole numbers without a decimal character Ex. ,”ELANGO\0”
point. 18.What is Escape sequences (or) Non-graphic
It may be signed (negative )or unsigned. Characters (or ) non- Printable Characters ?
Commas and blank spaces are not allowed. Non-printable characters cannot be typed directly
Three types : (i) Decimal (ii) Octal (iii) Hexadecimal from a keyboard .
Decimal These non-printable characters can be represented
Consists of digits (0 …. 9)Ex. 28 , -28 , 28.11 by using escape sequences.
Octal An escape sequence is represented by a backslash
Consists of digits (0.. 7)begins with 0 is considered ( \ ) followed by one or two characters.
as an Octal constant. Ex.022 , - 022
Hexadecimal
Consists of digits (0…9 ,A..F)
begins with 0x or 0X is considered as a Hexadecimal
constant. Ex. 0x28 , 0X3AC
13. What are Real constants (or) Floating point
constants
It is a numeric constant having a fractional
component.
It may be signed (negative )or unsigned with
decimal point
It may be written in fractional or in exponent form.
Exponent form consists of two parts:
(1) Mantissa and (2) Exponent.
The mantissa followed by a letter E or e and the
exponent.
14. How to write 58000000 . 00 in Exponent form.?
It may be written as 0.58 × 10 8 or 0.58E8.
Other Example:
5.864 E-1 - 5.864 × 10 1 - 58.64 Ex.\a ,\n , \t etc…
5864 E-2 - 5864 × 10 -2 - 58.64
0.5864 E-2 - 0.5864 × 10 2 - 58.64 19.How to represent a long and unsigned constant ?
15.What is Boolean Literals? The suffix L or l used to represent long constant
Used to represent Boolean values(True (1) or Ex.25L
false(0)). The suffix U or u used to represent unsigned
16.Differentiate between Character constant and constant. Ex. 25U
String constant? Evaluate Yourself
Character constant 1.What is meant by literals? How many types of integer
It must contain one character literals available in C++? Ref.9 & 11
must be enclosed within a single quote. 2. What kind of constants are following?
Ex. ‘p’ , ‘6’ , ‘+’ i) 26 ii) 015 iii) 0xF iv) 014.9
3. What is character constant in C++? Ref.16
Each single character constant has an equivalent
4. How are non graphic characters represented in
ASCII value. For ‘A’ is 65.
C++? Ref.18
String Literals
5. Write the following real constants into exponent
Set of characters are called String literals.
form: i) 32.179 ii) 8.124 iii) 0.00007
It enclosed within double quotes (“ “ ) .
Ans : i)32.179E3 ii)8.124E3 iii)0.7E-4
String should be terminated with a ‘\0’ (NULL)
6. Write the following real constants into fractional
character..
form: i) 0.23E4 ii) 0.517E-3 iii) 0.5E-5 iv)5E-5
Size of “welcome” is not 7 but 8 including \0 . Ans: i)2300 ii) 0.000517 iii) 0.000005 iv) 0.00005
Ex. “S” , “Elango” , “123” ,”ELANGO\0” 7. What is the significance of null (\0) character in a
string? Ref.17.
3
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
4
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
5
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
I/O Operators
6
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
50.Define Input operator or define Input statement . 52.What is the use of namespace std; or Define
The operator >> is called as “Stream extraction” or namespace std;
“get from” operator. It tells the compiler to use standard namespace.
>> operator to perform input operation. Namespace collects identifiers used for class ,object
It extracts the value through the keyboard and and variables.
assigns it to the variable on its right. Namespace provide a method of preventing name
It is a binary operator. conflicts in large projects.
The first operand is the pre-defined identifier cin It is introduced by the ANSI C++ standards
that identifies keyboard as the input device. committee.
The second operand must be a variable. 53.What are the importance of main() function?
Syntax : cin>>variable; ex. cin>>a; Every C++ program must have a main function.
cin>>var1>>var2>>var3; ex. cin>>a>>b; The main() function is the starting point where all
C++ programs begin their execution.
The executable statements should be inside the
main() function.
<< is called the “Stream insertion” or “put to” 54.How to creating and executing a C++ program
operator. (1) Creating Source code
<< operator to perform output operation.
Typing and editing the valid C++ code as per the
It is used to send the string or the values of the rules followed by the C++ Compiler.
variables on its right to the object on its left. (2) Saving source code with extension .cpp
<< is a binary operator. After typing, the source code should be saved with
The first operand is the pre-defined identifier cout the extension .cpp
that identifies monitor as the standard output
object. (3) Compilation
The second operand may be a constant, variable or In compilation, compiler links the library files with
an expression the source code and verifies each and every line of
code.
If any mistake or error is found, it will inform you to
make corrections.
Syntax : cout<<variable; Ex. cout<<a;
If there are no errors, it translates the source code
into machine readable object file with an
For yourself:
extension .obj
To display the contents of the variable:
(4) execution
Syntax : cout<<variable; Ex. cout<<a;
In this stage, the object file becomes an executable
To display the Message only
file with extension .exe.
Syntax : cout<<”Message”; Ex. cout<<”INDIA”;
An executable file, can run your application without
To display the Escape Sequence only
the help of any compiler or IDE.
Syntax : cout<<”\escape seq.”; Ex. cout<<”\n”;
55.What is IDE in C++?
To display the Message with Escape Sequence
Integrated Development Environment (IDE) makes
Syntax : cout<<”Message esc.seq.”;
it easy to create, compile and execute a C++
Ex. cout<<”INDIA\n”;
program.
To display the result of expression
cout<<a+b;
To display the constant or data.
cout<<7; or cout<< 2+3;
To display Message escape sequence variable
cout<<”Message”<<’\n’<<variable;
ex. cout<< “ The Result is..”<< ‘\t’ << c;
7
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
List some Familiar C++ Compilers with IDE 1.Describe the differences between keywords and
identifiers? Ref. 6 and 7
2. Is C++ case sensitive? What is meant by the term
“case sensitive”?
C++ is case sensitive as it treats upper and lower-
case characters differently.
3. Differentiate “=” and “==”.
4. Assume a=10, b=15; What will be the value of a^b?
5. What is the difference between “Run time error” and
“Syntax error”?
56.Explain how to work with Dev C++? 6. What are the differences between “Logical error”
Double click Dev C++ icon to open IDE and “Syntax error”?
To create a source file, 7. What is the use of a header file?
Select File → New → Source file or Press Ctrl + N. 8. Why is main function special?
After save, Click Execute → Compile and Run or 9. Write two advantages of using include compiler
press F11 key. directive.
If your program contains any error, it displays the 10. Write the following in real constants.
errors under compile log. (i) 15.223 (ii) 211.05 (iii) 0.00025
If your program is without any error, the display will
appear as follows.
After successful compilation, output will appear in Data Types, Variables and Expressions
output console.
57.Define Syntax error 60.What are the categories of Data types?
Syntax error occur when grammatical rules of C++ In C++, the data types are classified as three main
are violated. categories
Ex. cout<<”INDIA” - it will throw an error because (1) Fundamental data types
this statement does not end with a semicolon. (2) User-defined data types and
58.Define semantic error or logical error . (3) Derived data types
It may be happened by wrong use of variable /
operator /order of execution etc.
Here program is grammatically correct but it
contains some logical error.
59.Define run time error.
A run time error occurs during the execution of a
program.
It occurs because of some illegal operation that
takes place.
Evaluate Yourself….
1.What is meant by a token? Name the token available 61.Write about Fundamental data types.
in C++. Ref.5 Fundamental (atomic) data types are predefined
2. What are keywords? Can keywords be used as data types available with C++.
identifiers?ref.6 There are five fundamental data types in C++: char,
3. The following constants are of which type? int, float, double and void.
(i) 39 (ii) 032 (iii) 0XCAFE (iv) 04.14 int data type:
5. Assume n=10; what will be result of n>>2;? Integers are whole numbers without any fraction.
6. Match the following Integers can be positive or negative.
A B Integer data type accepts and returns only integer
(a) Modulus (1) Tokens numbers.
(b) Separators (2) Remainder of a division .Ex. int a = 5; int x = -4 ;
(c) Stream extraction (3) Punctuators int a= 4.5 (it will accept only 4)
(d) Lexical Units (4) get from
8
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
char data type: C++ compiler allocates specific memory space for
Character data type accepts and returns all valid each and every data .
ASCII characters. Every data is stored inside the computer memory in
Character data type is an integer type. the form of binary digits
All the characters are represented in memory by
their associated ASCII Codes.
Ex. ‘A’ represent 65
float data type:
Float data type accepts floating point values with 6
digits of precision.
It includes integer portion, a decimal
point ,fractional portion and an exponent.
Ex. float a =3.14;
There are two advantages of using float data types. 63.Write a short note on modifiers Or qualifiers .
(1) They can represent values between the integers. Modifiers can be used to modify (expand or reduce)
(2) They can represent a much greater range of values. the memory allocation of any fundamental data
Disadvantage type.
Floating point operations are slower than integer They are also called as Qualifiers
operations. There are four modifiers used in C++. They are:
double data type (1) signed (2) unsigned (3) long (4) short
double data type accepts double precision floating Integer type
point numbers with 14 digits of precision.
This type occupies double the space than float type
Ex. double a=3.456744;
void data type
It is used as a return type for functions that do not
return any value.
The void data type specifies an empty set of values.
It is used as a return type for functions that do not
return any value.
Ex. void main() , void add()
What are the advantages and disadvantages of float
data type ? Ref. above answer
Evaluate Yourself….
9
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
char type
Variables
65.Define variables.
Variables are user-defined names.
Variables are memory locations in which the values
are stored.
Variables are also identifiers.
These are called as symbolic variables because
these are named locations.
Floating point type
There are two values associated with a symbolic
variable;
They are R-value and L-value.
• R-value is data stored in a memory location
• L-value is the memory address .
The memory addresses are in the form of
Hexadecimal values
Ex.Let int a = 5 ; Here Variable name is a ;R value is
5 ; L value is 0x12b(for example)
10
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
11
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
12
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
13
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
10.Explain if …else statement with an example. If condition 1 is TRUE and Condition 2 is FALSE
Syntax: False block 2 will be executed
if (expression) Case3
{ If Condition 1 is FALSE
True – Block; False block 1 will be executed.
}
else 2. Nested if inside else part
{ Syntax:
False- block; if(condition-1)
} {
Statement – x ; TRUE block 1;
}
if the condition is true then a true block- is else
executed, False-block is skipped. {
if the condition is False then a true block- is if (condition-2)
skipped, False-block is executed. { True block 2 }
#Include<iostream> else
Using namespace std; Here,if condition { False block 2 }
int main() is True(Nonzero), }
{ Eligible for Vote 3. Nested if inside both if part and else part
int a Good Bye if(condition-1)
cin>>a; will be Display. {
if(a>=18) Otherwise control if (condition-2)
cout<<”Eligible for Vote”; jumps to elseand { True block 2 }
else Not Eligible for else
cout<<”Not Eligible for Vote”; Vote { False block 2 }
cout<<”\nGood Bye”; Good Bye. }
} will be executed. else
11.Explain Nested if statements with suitable example. {
An if statement contains another if statement is called if (condition-3)
nested if statement. It has three forms. { True block 3 }
1.Nested if inside if part else
2. Nested if inside else part { False block 3 }
3. Nested if inside both if part and else part }
12.Explain if -else-if ladder statement with an example.
1. Nested if inside if part The if-else ladder is a multi-path decision making
If(condition-1) statement.
{ In this type of statement,
if (condition-2) 'if' is followed by one or more else if statements
{ True block 2} and finally end with an else statement.
else Syntax:
{ False block 2} if(condition-1)
} { block – 1}
else else if (condition – 2 )
{ { block – 2}
False block 1; else if (condition – 3 )
} { block – 3}
else
{ default block}
14
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
Syntax:
switch (expression/variable) 16.Explain Nested switch statement.
{ When a switch statement contains another switch
case 1 : action block 1; break; statement is called as nested switch statement.
case 2 : action block 2; break; The inner switch and the outer switch constant may
default : action block 3; or may not be the same.
}
In the above syntax,
15
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
16
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
17
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
18
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
19
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
20
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
21
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
cout<<display();
21.Explain inline function with example }
An inline looks like a normal function in the source The name of the function is display(),
file but inserts the function’s code directly into the its return type is int and it does not have any
calling program. argument.
To make a function inline, insert the keyword The return statement returns a value of a (10) to
inline in the function header the calling function .
(Ex inline void swap (int a)) 3. A Function without return value and with parameter
inline keyword is just a request to the compiler #include <iostream >
Sometimes the compiler will ignore the request using namespace std;
Advantages of inline functions: void display( int a)
• Inline functions execute faster but requires more {
memory space. cout<<a;
• Reduce the complexity of using STACKS. cout<<"Without return value& with parameter ";
Ex. }
#include <iostream > int main()
using namespace std; {
inline void swap (int a) int x=10;
{ a=8; display(a);
cout << ‘\n’<< a; }
} The name of the function is display(),
int main ( ) its return type is void and it has one parameters (int
{ a).
int m1 = 10; 4. A Function with return value and with parameter
cout <<m1 ; #include <iostream >
swap (m1); using namespace std;
cout << ‘\n’<< m1; int display( int a)
} {
22.What are the different forms of user defined Int b=5
function ? Int c=a+b;
1. Function without return value and without cout<<"With return value& with parameter ";
parameter return c;
#include<iostream.h> }
void display() void main()
{ {
cout<<"No return value& without parameter "; int x=10;
} cout<< display(a);
void main() }
{ The name of the function is display(), its return type
display(); is int and it has one parameter.
} The return statement returns with c value to the
In the above program, The name of the function is calling statement.
display(), its return data type is void and it does not 23.Define return statement in C++.
have any argument. The return statement is used to return from a
2.A Function with return value and without parameter function to the calling function.
#include <iostream > It is a jump statement.
using namespace std; A return may or may not have a value associated
int display() with it.
{ A return statement without parameter can be used
Int a =10; to terminate the function.
cout<<"With return value& without parameter "; Syntax: return expression/variable;
return a; Example : return(a+b); return(a); return;
}
void main()
{
22
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
23
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
24
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
cout<< "Enter the name of the country: "; Ex. int n [2] [3] = (2 x 3) x 2 = 12 bytes
cin>>country; 15.How to initialize a two dimensional array .
cout<<" The name of the country is "<<country; The array can be initialized in more than one way at
} the time of 2-D array declaration.
OUTPUT Ex. int a [2][2] = { {1,2},{3,4}};
Enter country the name: INDIA Array’s row size is optional but column size is
The country name is INDIA compulsory.
9.How to Initialize one dimension character array ? Accessing the two-dimensional array
The character array can be initialized at the time of A[0][1] = 10; assign 10 to 2nd element of first row
its declaration. The syntax is shown below: 16.Define Memory representation of 2-D array
char a_nam[size]={ list of characters separated by A 2-D array is stored in sequential memory blocks.
comma or a string } ; There are two types of 2-D array memory
char country[6]=“INDIA”; representations. They are:
char country[6]={‘I’, ‘N’, ‘D’, ‘I’, ‘A’, ‘\0’}; Row-Major order
char country[]=“INDIA”; In row-major order, all the elements are stored row
char country[]={‘I’, ‘N’, ‘D’, ‘I’, ‘A’, ‘\0’}; by row in continuous memory locations,
Column-Major order
During initialization, the array of elements cannot In column-major order, all the elements are stored
be initialized more than its size. column by column in continuous memory locations,
char str[2]={'5','+','A','B'}; // Invalid 17.What is array of string?
An array of strings is a two-dimensional character
10.Define cin.get(). array.
In C++, cin.get() is used to read a line of text The size of first index (rows) determines the number
including blank spaces. of strings and the size of second index determines
This function takes two arguments. maximum length of each string.
The first argument is the name of the string and Ex. char day[2][10] ={“Sunday \0”,“Monday\0”};
second argument is the maximum size of the array. S u N d a Y \0
char str[100]; M O N D A Y \0
cin.get(str, 100); 18.Write a C++ program to accept and print your
Name.
11.Define cin.getline() #include <iostream>
In C++, getline() is also used to read a line of text using namespace std;
including blank spaces from the input stream. int main()
It can read the characters till it encounters a newline {
character or a delimiter specified by the user. char n[]=”ELANGO”;
This function is available in the <string.h> header. cout<<”Myname is …”<<n;
12.Define Two-dimensional array }
Two-dimensional (2D) arrays are collection of 19.How will you pass two dimensional array to a
similar elements where the elements are stored in function explain with example?
certain number of rows and columns. In C++, arrays can be passed to a function as an
ex.int arr[3][3]; argument.
13.How to Declare 2-D array in C++? Write the syntax The actual parameter is passed only the array
of Declaration of 2-D array. name as an argument ignoring dimensions.
data-type array_name[row-size][col-size]; Passing a two-dimensional array to a function
Ex. int a[3][4]; #include <iostream>
Array size must be Positive integer value using namespace std;
In arrays, column size is compulsory but row size is int main()
optional. {
14.How to calculate the size of the array? int marks[5]={88, 76, 90, 61, 69};
one dimension array size = memory required (data display(marks);
type ) x No. of the elements in the array }
Ex. int n[5] = 2 x 5 = 10 bytes ( one integer is 2 bytes)
Two dimension array = Number of elements (Row x
column ) x memory required
25
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
26
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
29
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
30
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
31
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
32
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
34
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
26.Define constructor overloading. Types of 29.What are the ways to create an object using
constructor. parameterized constructor?
Function overloading can be applied for There are two ways to create an object using
constructors, are called constructor overloading . parameterized constructor 1)Implicit call 2) Explicit call
Three types of constructors, they are Implicit call
1.Non-Parameterized constructor or default constructor In this method ,the parameterized constructor is
2.Parameterized constructor invoked automatically whenever an object is
3.copy constructor created.
27.Define Non-Parameterized constructor or default For example simple s1(10,20);
constructor. In this for creating the object s1 parameterized
A constructor that accepts no parameter is called constructor is automatically invoked.
default constructor. Explicit call
In the absence of user defined constructor, the In this method , the object can be created and
compiler automatically generates default initialized
constructor when the object is created For example simple s1=simple(10,20);
A default constructor implicitly as an inline public An explicit call to constructor creates temporary
member. instance(object).
Advantage :
Default constructors are very useful to create 30.Explain copy constructor with an example.
objects without having specific initial value. Copy Constructor is a type of constructor which is
It is also used to create array of objects. used to create a copy of an already existing object of
Ex. a class type.
class add While defining copy constructor the argument
{ (object) should be passed only by reference not by
public: value method.
add(); Ex, simple (simple&x) - simple- class name.
{
} Calling copy constructors
}; A copy constructor is called
int main() 1.When an object is passed as a parameter to
{ any of the member functions
add x; Example:void simple::putdata(simple x);
} 2.When a member function returns an object
28.Define Parameterized Constructors Example:simple getdata() { }
A constructor with arguments is called 3.When an object is passed by reference to an
parameterized constructor . instance of its own class
This type of constructor helps to create objects with Example:simples1, s2(s1); // s2(s1) calls copy
different initial values. constructor
This is achieved by passing parameters to the
function. 31.How constructors are executed (Order of
Parameterized constructor can havedefault constructor invocation) ?
arguments. The constructors are executed in the order of the
Ex. object declared.
class add For example
{ Test t1; Test t2;
public: The order of constructor execution is first for t1 and
add(int a) then for t2.
{ Sample s1,s2,s3 ; //The order of construction is s1 then
} s2 and finally s3
};
int main() But if a class containing an object of another class as
{ its member then that class constructor is executed
add x(5); first.
}
35
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
******************
36
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
37
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
10.class sale ( int cost, discount ;public: sale(sale &); 15.Define and explain the operator overloading?
Write a non inline definition for constructor specified; Define :
sale :: sale(sale &a) The mechanism of giving special meaning to an
{ operator is known as operator overloading.
cost=s.cost; Operator overloading provides new definitions for
discount=s.discount; most of the C++ operators
} The definition of the overloaded operator is given using
the keyword 'operator' followed by an operator symbol.
11.Define Operator overloading Syntax:
The mechanism of giving special meaning to an Inline:
operator is known as operator overloading. ReturnType operator operatorSymbol(argument)
Operator overloading provides new definitions for {
most of the C++ operators }
12.List out the operators that cannot be overload in Ex. complex operator +( complex c2)
C++ {
scope operator (:: ) }
sizeof Outline:
member selector ( . ) ReturnType classname : : operator operatorSymbol(argument)
member pointer selector (* ) {
ternary operator ( ?: ) }
13.How to define operator overload in C++ Rules or Restrictions on Operator Overloading
The definition of the overloaded operator is given Precedence and Associativity of an operator cannot
using the keyword 'operator' followed by an be changed.
operator symbol. No new operators can be created,
Syntax: Only existing operators can be overloaded.
Inline: Cannot redefine the meaning of an operator’s
ReturnType operator operatorSymbol(argument) procedure.
{ Overloaded operators cannot have default
} arguments.
Ex. complex operator +( complex c2) When binary operators are overloaded, the left
{ hand object must be an object of the relevant class
}
Outline:
ReturnType classname : : operator operatorSymbol(argument)
{
}
38
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
39
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
7.Differentiate between access specifier and visibility When classes are inherited the private members of
modes the base class are not inherited they are only
Access specifiers control the accessibility of the visiblei.e continue to exist in derived classes, and
class members with in the class cannot be accessed
Visibility modes control the access of inherited 9.Differentiate between private and public visibility
members with in the class modes.
8.Explain the different visibility mode in inheritance. Ref.above ans.
The accessibility of base class by the derived class is 10.What is the size of following class?
controlled by visibility modes. classx
The three visibility modes are private, protected and {
public. };
The default visibility mode is private. A class without any declaration will have 1 byte size.
Private visibility mode So ,x occupies 1 byte.
When a base class is inherited with private visibility,
The public and protected members of the base class 11.Explains the significance of different visibility modes
become ‘private’ members of the derived class from following program.
#include <iostream>
using namespace std;
class Shape
{
private:
int count;
protected:
int width;
Protected visibility mode int height;
When a base class is inherited with protected visibility. public:
The protected and public members of the base class voidsetWidth(int w)
become ‘protected members ‘ of the derived class {
width = w;
}
voidsetHeight(int h)
{
height = h;
}
};
class Rectangle: public Shape
Public visibility mode {
When a base class is inherited with public visibility, public:
The protected members of the base class will be intgetArea()
inherited as protected members of the derived {
class. return (width * height);
The public members of the base class will be }
inherited as public members of the derived class. };
int main()
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
cout<< "Total area: "<<Rect.getArea() <<endl;
return 0;
}
Output
Total area: 35
40
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
The following table contain the members defined 13.Find the output of the following program.
inside each class before inheritance #include<iostream>
using namespace std;
classbase
{
public:
base()
{
cout<<"\nConstructor of base class...";
The following table contain the details of members }
defined after inheritance ~base()
{
In case the class rectangle is derived with private cout<<"\nDestructor of base class.... ";
visibility mode }
};
classderived:public base
{
public :
derived()
{
cout<< "\nConstructor of derived ...";
}
~derived()
Suppose the class rectangle is derived with protected {
visibility cout<< "\nDestructor of derived ...";
}
};
class derived1 :public derived
{
public :
derived1()
{
cout<< "\nConstructor of derived1 ...";
Suppose the class rectangle is derived with }
publicvisibility ~derived1()
{
cout<< "\nDestructor of derived1 ...";
}
};
int main()
{
derived1 x;
return 0;
12.WriteSome Facts about the execution of constructor }
in inheritance. Output:
Base class constructors are executed first ,before Constructor of base class...
the derived class constructors execution Constructor of derived ...
Derived class can not inherit the base class Constructor of derived1 ...
constructor. Destructor of derived1 ...
If there are multiple base classes ,then its start
Destructor of derived ...
executing from the left most base class
In multilevel inheritance, the constructors will be
Destructor of base class....
executed in the order of inheritance
41
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
42
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
43
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
} int b1:b2:b3;
B() A::getdata[];
{ b1=a1;
cout<<endl<<" I am class B "<<endl; b2=a2;
} a3=a3;
~B() cout<<b1<<’\t’<<b2<<’t\’<<b3;
{ }
cout<<endl<<" Bye "; void main()
} {
void show() clrscr()
{ B der;
cout<<"x = "<<x<<endl; der1:func();
cout<<"y = "<<y<<endl; getch();
} }
}; To Know Yourself
int main() Define Pointer variable
{ A pointer is a variable that hold a memory address
AobjA; of other variable
B objB(30, 20); In Pointer ,the memory location of a variable can be
objB.show(); directly accessed.
return 0; The address of ( & ) and the value at operator (*)are
} deals with pointer.
Output: To declare
I am class A Syntax :data type *Variable name;
I am class B Ex. Int *abc;- Here The variable abc can only
X=30 store addresses.
Y=20 Initialization of pointer variable:
Bye Int *abc , n;
Bye n=10;
20.Debug the following program abc = &n;
Output Pointer variable can store the address of other
------------- variables
15 Pointer variable and assigning variable should have
14 same data type.
13 ---------------------------------------------------
Program : For example
------------- Int *abc ; float n;
%include(iostream.h) n=10.5;
#include<conio.h> abc = &n;
Class A Compiler shows an error because,
{ Pointer variable and assigning variable should have
public; same data type
int a1,a2:a3; ==============================================
Void getdata[]
{
a1=15;
a2=13;a3=13;
}
}
Class B:: public A()
{
PUBLIC
voidfunc()
{
44
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
45
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
46
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
2.Define e – Governance:
Getting Government services through internet is known
as e-Governance.
One can get important announcements, government
orders, and government welfare schemes from the web
portal of Govt.
Ex.www.tn.gov.in\ta
3.Define e-Library
E-Libraries are portal or website of collection of e-
books.
E-Library services provide thousands of Books as e-books
mostly at free of cost.
Ex.www.chennailibrary.com
47
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
www.nammakalvi.in
11
COMPUTER SCIENCE
PRACTICAL
9677515019
www,rmkmhsselango.blogspot.com
Elangovan M.Sc,B.Ed,M.Phil
Head of the Dept.
R.M.K.Matric.Hr.Sce.School,
R.S.M.Nagar,601206.
48
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
49
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
CS2 - PERCENTAGE
#include<iostream>
using namespace std;
int main()
{
int p,x;
cout<<"Enter Your Percentage:\t";
cin>>p;
Output 1
cout<<"\n\n\nYou Scored "<<p<<"%\n"; Enter your percentage: 79
x=p/10; You scored 79%
First division
switch(x) Output 2
Enter your percentage: 39
{
You scored 39%
case 10: case 9: case 8: Sorry: You have failed
cout<<"\nDistinction";break;
case 7: case 6 : cout<<"\nFirst
division";break;
case 5:cout<<"\nSecond division";break;
case 4: cout<<"\nThird division";break;
default: cout<<"\nSorry , You have
Failed..";
}
}
50
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
CS3 - PALINDROME
51
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
52
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
53
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
54
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
Output 1
Output 2
--------Menu-----------
1.Insert
2.Delete
3.Exit
-----------------------
Enter your choice: 2
Enter the position: 2
The deleted element is = 2
The array elements are:
1 3 26 4 5
55
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
56
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
57
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
void display()
Create a C++ program to create a class employee {
cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<
containg the following members <hra<<"\t"<<da<<"\t"<<pf<<"\
t"<<np<<"\n";
AIM: To create a class employee containg the }
following members };
Coding int main(){
#include<iostream> int i, n;26
using namespace std; char ch;
class emp{ salary s[10];
public: cout<<"Enter the number of employee:";
int eno; cin>>n;
char name[20], des[20]; for (i =0; i < n; i++){
void get(){ s[i].get();
cout<<"Enter the employee number:"; s[i].get1();
cin>>eno; s[i].calculate();
cout<<"Enter the employee name:";25 }
cin>>name; cout<<"\n\t\t\tEmployee Details\n";
cout<<"Enter the designation:"; cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf
cin>>des; \t np \n";
} for (i =0; i < n; i++){
}; s[i].display();
class salary :public emp }
{ return 0;
float bp,hra, da,pf,np; }
public: Output
void get1() Enter the number of employee:2
{ Enter the employee number:1201
cout<<"Enter the basic pay:"; Enter the employee name:Ramkumar
cin>>bp; Enter the designation:Engineer
cout<<"Enter the HouseRent Allowance:"; Enter the basic pay:50000
cin>>hra; Enter the House Rent Allowance:10000
cout<<"Enter the Dearness Allowance :"; Enter the Dearness Allowance :5000
cin>>da; Enter the Provident Fund:1000
cout<<"Enter the Provident Fund:";
cin>>pf; Enter the employee number:1202
} Enter the employee name:Viswanathan
void calculate() Enter the designation:Engineer-Tech
{ Enter the basic pay:40000
np=bp+hra+ da -pf; Enter the House Rent Allowance:9000
} Enter the Dearness Allowance :4500
Enter the Provident Fund:1000
58
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
59
www.rmkmhsselango.blogspot.com 9677515019
Dept.of C.S. ,RMKMHSS - 601206
60
www.rmkmhsselango.blogspot.com 9677515019
www.nammakalvi.in Dept.of C.S. ,RMKMHSS - 601206
61
www.rmkmhsselango.blogspot.com 9677515019