3.C++ 2024
3.C++ 2024
DO
3. C++
KA
12th Computer Science
Maharashtra Board
DE
CO
SE
DO
Q.2 Differentiate between the
traditional procedural programming approach and object-
oriented programming approach.
KA
DE
CO
Traditional Procedural programming Object Oriented Programming Approach
SE
In this approach, the problem is viewed as In this approach, is decomposed into a
a sequence of things to be done. number of entities called objects and then
DO
builds data and function around this entities.
KA
Large programs are Programs are divided into entities
divided into smaller programs known known as objects.
as functions.
DE
Data move openly around the system from Data is hidden and cannot be accessed by
external functions.
Employs top-down Follows bottom-up approach in program
CO
approach in program design. design.
Q.6 Enlist the basic data types used in C++ with size of data
SE
in terms of bytes for each.
There are three main basic built-in data types used in C++ viz. integral type,
DO
void and floating type.
KA
DE
CO
SE
1. Integral data type:
• integer (int) - 2 Bytes
DO
• Integer variables are also of two types :
(a) short int (2 bytes).
(b) long int (4 bytes)
KA
• character (char) - 1 Bytes
DE
CO
2) Void data type:
SE
Void data type issued :
DO
• to specify the return type of a function when it is not
returning any value.
• to indicate an empty argument list to a function.
KA
• to declare generic pointers.
DE
CO
3) Floating type :
SE
• Floating type variables are of two types;
DO
1. float (4 Bytes)
2. double. (8 bytes )
3. long double (10 bytes )
KA
DE
CO
Sr. Type Bytes Range
SE
No.
1 char (Signed char) 1 -128 to 127
DO
2 unsigned char 1 0 to 255
3 int (short int or signed int) 2 -32768 to 32767
KA
4 unsigned int 2 0 to 65535
4 Long Int 4 0 to 65535
5 float 4 3.4 × 10-38 to 3.4 × 10-38
6 double
DE 8 1.7 × 10-308 to 1.7 x 10308
CO
SE
1. A typical C++ program contains 4 sections as shown in
DO
following figure These sections may be placed in different code
files and then compiled independently or jointly.
Include files
KA
Class declaration
Class functions definitions
DE Main function program
CO
Structure of C++ program
CO
DE
KA
DO
SE
SE
1. it is a common practice to organize a program into three separate files.
2. The class declarations are placed in a header file and the definitions of
DO
the member go in other file.
KA
4. Finally the main program that uses the class is placed in third file, which
includes the previous two files as well as any other files required.
DE
CO
Write a C++ program to find factorial of a natural number
SE
input during program execution.
DO
KA
DE
CO
What are pointers ?Give the advantages of using pointers.
SE
DO
1. A" pointer is a variable, which holds the memory address of another
variable.”
2. * operator is used to declare pointers in C++. It takes the form as
KA
datatype *variable name;
3. e.g. int *ptr;
4. The above declaration will create a variable ptr, which is a pointer variable
and which will point to a variable, whose data type is integer.
5. DE
The data type of ptr is not integer, but data type of variable which ptr will
point si integer.
CO
SE
Advantages of using pointers are as :
DO
1. It allows to pass variables, arrays, functions, strings, structures,
objects as function arguments.
2. It allows to return structured variables from functions.
3. It supports dynamic allocation and deallocation of memory
KA
segments.
4. By using pointers, variables can be swapped, without physically
moving them.
DE
5. It allows to establish link between data elements or objects.
CO
Write a C++ program to accept a set of 10 numbers and print the
SE
numbers using pointers.
DO
KA
DE
CO
What is object-oriented programming? Enlist the features of
SE
object-oriented programming.
Definition of OOP :
DO
1. Object-oriented programming (OOP) is a way of making programs by dividing
them into small parts that can hold both information and the actions that work
on the information.
KA
2. These parts are like blueprints or templates to make many copies as needed.
3. In OOP, we focus on the information we're working with instead of just the
actions. It helps connect information more directly with the actions that use it.
DE
4. OOP breaks down a problem into objects, which are parts that have
information and actions to deal with that information.
5. When the program runs, these objects can talk to each other by sending
CO
messages. Each object has its own information and the actions to change that
information.
SE
DO
Car Example:
● Object: Car
● Information/Data: Color, brand, speed
● Actions/Functions: Drive, brake, accelerate
KA
● In OOP, a car is an object that has data like its color, brand, and
speed. It also has functions such as to drive, brake, and accelerate.
We can create many cars with different properties using the same
blueprint. DE
CO
SE
Features of OOP:
DO
2. Programs are divided into number of objects.
3. Data structures are designed such that they characterize the
objects.
KA
4. Functions that operate on the data of an object are tied together in
the data structure.
5. Data is hidden and cannot be accessed by external functions.
DE
6. Objects may communicate with each other through functions.
CO
SE
declaration.
1. Class is a way to bind data and its associated function together
DO
2. It allows the data (and functions) to be hidden, if necessary, from external
use.
3. When defining a class, a new abstract data type can be created that is
treated like any other built-in data type.
KA
4. Generally, a class specification has two parts :
1. Class declaration.
2. Class functions definition.
5.
6.
DE
The class declaration describes the type and scope of its members. class
function definitions describe how class functions are implemented.
Class declaration :
CO
CO
DE
KA
DO
SE
a) The keyword class specifies that what follows is an abstract data
SE
type class-name.
b) The body of a class is enclosed within braces and terminated by a
DO
semicolon.
c) The body of the class contains declaration of class members, which
are variables and functions. They are generally grouped under two
sections namely private and public, which are known as visibility
KA
labels. These keywords are followed by a colon.
d) The members, declared as private can be accessed only from
within the class. It hides data from external use. It is a key feature
of OOP. DE
e) The public members can be accessed from outside the class also.
f) If both the visibility labels are missing, then by default, the members
CO
of the class are private. Such a class is completely hidden from
outside world and does not serve any purpose.
Example : A class declaration would look like :
SE
DO
KA
DE
CO
Describe how member functions of class can be defined
SE
outside the class definition and inside the class definition.
Member functions of a class can be defined in two places :
DO
(i) Outside the class definition.
(ii) Inside the class definition
KA
Irrespective of the place of definition, the function performs the same
operation.
Hence code for the function body is identical in both cases.
DE
CO
Outside the class definition :
SE
i.
● The general form of member function definition outside the class definition is:
DO
return-type class-name : : function-name (Argument declaration)
{
function body
KA
}
DE
CO
• The member function incorporates an identity label or membership label
SE
(i.e. class-name: :).
• This label tells the compiler the class to which the function belongs and
restricts the scope of that function to the object of the class ‘class-name’
DO
specified in the header line.
e.g.
KA
DE
CO
(ii) Inside the class definition :
SE
1) Another method for. defining a member function is to replace the
function declaration by the actual function definition.
2) When a function is defined inside a class, it is treated as an inline function.
DO
Normally, only small function are defined inside the class definition.
e.g.
KA
DE
CO
What is inheritance? Explain with suitable example.
SE
● The mechanism of deriving a new class from an old one is called
as inheritance.
DO
● The old class is referred as base class and new class is referred
as derived class.
● C++ strongly supports the concept of reusability. Once a class
has been written and tested, it can be adapted by other
KA
programmers to suit their requirements.
● This is basically done by creating new classes, reusing the
properties of the existing ones.
●
●
DE
Functions and variables of a class that has been tested can be
used by object of another class. This is known as inheritance.
The reuse of a class that has already been tested, debugged and
CO
used many times, can save the efforts of developing and testing
the same again.
SE
Single inheritance
DO
● A derived class with only one base class is called as single
inheritance.
KA
DE
CO
SE
Multilevel inheritance :
DO
● The mechanism of deriving one class from another derived class is
multilevel inheritance.
KA
DE
CO
SE
Multiple inheritance :
DO
● When a class is derived from several base classes, it is called as
multiple inheritance.
KA
DE
CO
SE
Hierarchical inheritance :
DO
● The traits of one class may be inherited by more than one classes.
This process is known as hierarchical inheritance.
KA
DE
CO
SE
Hybrid inheritance :
DO
● The inheritance which involves more than one inheritances is called
as hybrid inheritance. For eg. : Above figure involves hierarchical,
multiple and multilevel inheritances and the resultant inheritance is
called hybrid inheritance.
KA
DE
CO
SE
What are friendly functions? Give the characteristic of a
friend function?
DO
1) C++ allows the common function to be made friendly with more than
one classes, thereby allowing the function to have access to the private
data of classes. Such a function need not be a member of any classes.
KA
2) Non-member function cannot have access to the private data of class.
However, there could be a situation where user would like two classes
to share a particular function. At this situation friend function is used.
DE
3) To make an outside function “friendly” to a class, simply declared
CO
the function as the friend of the class as shown below :-
CO
DE
KA
DO
SE
SE
4 The keyword “friend’ declares the function to be friendly with that class.
This function is defined as a normal C++ function. The function definition
does not use class-name, keyword friend or scope resolution operator.
DO
5 A friend function has following characteristics :
a) It is not in the scope of the class to which it has been declared as friend.
b) Since it is not in scope of the class, it cannot be called by using object of
KA
that class. It is called like a normal C++ function.
c) It can be declared either in public or the private part of a class
without affecting its meaning.
DE
d) Usually, it has the objects as arguments.
e) It cannot access the member function directly and has to use an object
CO
name and dot operator with each member name.
CO
DE
KA
DO
SE
Explain in short the three special characteristics of a static
SE
data member in a class
• Single Copy: There is only one copy of a static data member, regardless of how
DO
many objects of the class are created. This single copy is shared among all instances
of the class.
• Class Scope: Static data members are associated with the class rather than any
particular object instance. They can be accessed using the class name instead of an
KA
object of the class. For example, if ClassName is a class with a static data member
staticMember, you can access it as ClassName::staticMember.
• Initialization: Unlike regular data members, static data members must be explicitly
DE
defined and initialized outside the class definition, typically in a .cpp file. This is
because they are not part of class objects but rather belong to the class itself. The
initialization syntax does not include the static keyword. For example, int
CO
ClassName::staticMember = initialValue;.
SE
• Access Control: Static data members can be public, protected, or private, just like
DO
any other class members. This controls who can access the static member. For
example, a private static data member can only be accessed by member functions
of the class.
• Lifetime: The lifetime of a static data member is the entire duration of the
KA
program. It is created when the program starts and destroyed when the program
ends.
• Usage: Static data members are often used for constants that are the same for all
DE
objects of a class, counters that keep track of the number of class instances, or
other shared resources that need to be accessed by all instances of a class or by the
class's static methods.
CO
CO
DE
KA
DO
SE
What is a constructor ? Why it is called so ?
SE
1) “A constructor is a special member function of a class. Its task is to
initialize the objects of its class.”
DO
2) It is special because its name is same as that of the class to which it
belongs.
3) The constructor is invoked whenever an object of its associated class is
KA
created.
4) It is called constructor bec`ause it constructs the values of data
members of the class.
DE
5) A constructor can never return any value. Hence, it is written with no
return type (even void is not written).
6) e.g. A constructor is declared and defined as follows
CO
7) Whenever a class contains a constructor like one above, it will be
initialized automatically, whenever an object of that class is created.
CO
DE
KA
DO
SE
Give the characteristics of constructor function. OR
SE
What are the syntax rules for writing constructors ?
DO
1. The constructor name is always Same as the class name.
2. They do not have return types, not even void and therefore, they
cannot returns values.
KA
3. They cannot be static or Virtual.
4. They should be declared in the public section.
5. They cannot be inherited though a derived class can call base
class construct functions.
DE
6. Like other C++, they can have default arguments.
CO
What is a destructor? Give syntax rules for writing
SE
destructor function.
DO
1. A destructor, as the name implies, is used to destroy the objects that
have been created by a constructor.
KA
2. The destructor is invoked implicitly by the compiler upon exit from
the program to clean up storage that is no longer accessible.
SE
1. A destructor function name is same as that of its class name. But it
is preceded by a tilde
e.g. ~fib( ){.... message .....}
DO
2. It is declared with no return type since it can never return any value.
3. It takes no arguments.
4. It should have public access in the class declaration.
KA
DE
CO
What is polymorphism ? Explain runtime and compile time
SE
polymorphism
1. "Polymorphism refers to identically named methods (member
DO
functions) that have different behavior depending on the type of object
they refer.”
2. Polymorphism simply means “one name, multiple forms.”
KA
3. The types of polymorphisms and their examples are shown in
following figure.
DE
CO
Compile time polymorphism :
SE
• Function overloading and operator overloading are the examples of
compile time polymorphism.
DO
• In this case, the overloaded member functions are selected for invoking
matching arguments, both type and number.
• This information is known to the compiler at the compile time and,
KA
therefore the compiler is able to select the appropriate function for a
particular call at the
• compile time itself. This is known as compile time polymorphism.
•
•
binding or DE
Compile time polymorphism is also called as early binding or static
static linking. Early binding simply means that an object is bound to its
CO
function at compile time.
Run time polymorphism :
SE
• In some situations, it is nice to select appropriate member function to be
invoked while the program is running. This is known as runtime
polymorphism.
DO
• e.g. consider a situation where the function name and prototype is the
same in both the base and derived classes as shown in following class
definitions.
KA
DE
CO
SE
Here, show( ) function is used to print values of object of both the classes A
and B. The prototype of show( ) is the same in both the places, the function
is not overloaded and therefore static binding does not apply.
DO
• In such situations, the appropriate member function can be selected at
runtime and it is known as runtime polymorphism.
• To achieve runtime polymorphism, C++ supports mechanism of
KA
virtual functions.
• At runtime, it is known what class objects are under consideration, the
appropriate version of function is called.
• Since the function is linked with a particular class much after its
DE
compilation, this process is termed as late binding. It is also called as
dynamic binding because the selection of the appropriate function is done
CO
dynamically at runtime.
Explain the concept of function overloading with example.
SE
• The use of the same function name to create functions that perform a
DO
variety of different tasks is called as function overloading.
• Overloading refers to the use of same thing for different purposes.
Function overloading or function polymorphism, is an example of compile
time polymorphism.
KA
• Using the concept of function overloading, create a family of functions with
one function name but with different argument lists.
• The function would perform different operation, depending on argument
• DE
list in function call.
The correct function to be invoked is determined by checking the number
and the type of the arguments and not on the function type.
CO
CO
DE
KA
DO
SE
SE
• In above example the function area () is overloaded.
DO
• The first function is used to calculate area of square. It has one
integer parameter.
• The second function is used to calculate area of rectangle. It has
two integer parameters.
KA
• When a function is called, the compiler first matches the prototype
having same number and types of arguments and then calls
appropriate function for execution.
• DE
A best match must be unique.
CO
State any eight rules for overloading operators.
SE
There are certain restrictions limitations for overloading operators. Some
DO
of them are listed below :
• Only existing operators can be overloaded. New operators cannot be
created.
KA
• The overloaded operator must have atleast one operand that is of
user-defined type.
• The basic meaning of an operator cannot change. i.e. we cannot
redefine the
DE
• Pls (+) operator to subtract one value from the another.
• The overloaded operators follow the syntax rules of original
CO
operators.
Following are some operators that cannot be overloaded.
SE
DO
KA
Following certain operators cannot be overloaded using friend functions
but member functions can be used to overload them.
DE
CO
SE
• Unary operators, overloaded by means of a member function, take
no explicit arguments and return no explicit values.
• Unary operators, overloaded by means of a friend function take
DO
one reference argument.
• Binary operators overloaded through a member function take one
explicit argument.
KA
• Binary operators overloaded through a friend function takes two
explicit arguments.
• When using binary operators overloaded through a member
•
class. DE
function, the left hand operand must be an object of the relevant
SE
• When user use the same function name in both the base and derived
classes, the function in base class is declared as virtual using the
DO
keyword ‘virtual’ preceding its normal declaration.
• When a function is made virtual, C++ determines which function to use
at runtime, based on the type of object pointed to by the base pointer.
• Thus, by making the base pointer to point two different objects, it can
KA
execute different versions of the virtual function.
• Virtual functions can be accessed aca the use of a pointer declared as a
pointer to the base class.
•
DE
Also, the prototypes of the base class version of a virtual function and all
the derived class versions must be identical.
• If two functions with the same name have different prototype, C++
CO
considers them as overloaded functions, and the virtual function
mechanism is ignored.
CO
DE
KA
DO
SE
State any eight basic rules for virtual functions that satisfy
SE
the compiler requirements.
When virtual functions are created for implementing late binding, we should
DO
observe following basic rules that satisfy the compiler requirements :
KA
• They are accessed by using object pointers.
• A virtual function can be a friend of another class.
• A virtual function in a base class must be defined, even though it is not
•
used.
DE
The prototypes of the base class version of virtual function and all
derived class version must be identical. If two functions have different
CO
prototypes, then C++ considers them as overloaded functions and not as
virtual functions.
SE
• We cannot have virtual constructors, but we can have virtual destructors.
• A base pointer can point to any type of derived object, the reverse is not
true. i.e. we cannot use a pointer to derived class to access an object of
DO
the base type.
• When base pointer points to derived class, the incrementation
and decrementation is only relative to its base type.
KA
• Virtual functions are defined in base class, they need not be redefined
in derived class.
DE
CO
What is the function of each of each of the following file
SE
stream classes ?
DO
• ifstream : Provides input operations. This file stream class is used to
read a stream of objects from a file.
KA
stream of objects in a file.
• filebuf : Its purpose is to set the file buffers to read and write. It contains
DE
close( ) and open () as members.
CO
CO
DE
KA
DO
SE
How we can open a file using open() function ?
SE
We can open files using open( ) by two ways :
1) In first method, open( ) function takes only one argument and that is file
DO
name.
This is done as follows : file-stream-class stream_object;
Stream_object.open("file name");
KA
e.g. ofstream fout;
fout.open(“Try”) ;
2) A file can be also opened using open() by passing two arguments. The
DE
first argument is file name and the second argument is used to specify the
file mode. The general form of open() with two arguments is,
CO
stream_object.open(“file name”, mode);
SE
The second argument mode specifies the purpose for which file is
opened. - e.g. fout.open(“computer”, ios::app);
DO
This opens the file in append mode.
KA
file-stream-class-stream-object (“file name”);
e.g. ofstream outf (“computer”)
DE
It will open file computer in output mode
CO
Explain the Purpose of following functions with example :
SE
• Seekg() : This function is used to move the file pointer forwards, with
given number of bytes. It has the form
DO
Seekg (unsigned int);
e.g. ifstream inf(“xyz.dat”);
inf.seekg(10);
KA
In above example, seekg() moves input pointer 10 bytes forward.
seekg is associated with an input pointer or get pointer.
DE
CO
SE
• Seekp() :
This function is used to reposition file pointer to a given number of
DO
bytes. This function is associated with output pointer. Corresponding
class to process this function is “ofstream’” class.
KA
seekp (unsigned int);
e.g. ofstream outf(“xyz.dat”);
DE outf.seekp(10)
SE
e.g: ifstream inf;
DO
inf.open(“xyz.dat”);
In above example, tellg( ) will return value zero,
int because
n; initially, input
pointer points to zeroth location. n=inf.tellg( );
KA
DE
CO
SE
• tellp() : This function is used to return current file pointer (output
pointer) position. It is associated with output file stream.
DO
e.g. ofstream outf;
outf.open(“xyz” ,ios::app);
int n=outt.tellp();
KA
DE
In above example file is opened in append mode,
therefore file pointer points to end-of-file character.
Hence, tellp( ) returns number of characters present
CO
in file xyz.
SE
Note: “Seek” functions can also be used with two arguments as :
seekg(offset, ref position),
DO
seekp(offset, ref position);
Offset represents number of bytes file pointer is to be moved from the
location specified by ref position.
KA
The ref position can be one of the three constants :
(i) ios:-beg - start of file
(ii) ios::cur - current position of pointer
DE
(iii)i os::end - end-of-file.
CO
Explain the three types of data conversion in C++ with a
SE
suitable example.
Three types of data conversion in C++ are as follows :
DO
i. Conversion from built-in type to class type.
ii. Conversion from class type to built-in type.
iii. Conversion from one class to another class.
KA
Basic to class type:
The constructor can be used for default type conversion from argument’s
DE
type to the constructor’s class type.
CO
CO
DE
KA
DO
SE
Class to basic type :
SE
Overloaded casting operator is used to convert a class type data to basic
type. The general form is as : operator typename ( )
{
DO
…........... (function
statement)
…...........
KA
}
The conversion function must satisfy following conditions :
(a) It must be a class member.
DE
(b) It must not specify a return value.
(c) It must not have any arguments.
CO
CO
DE
KA
DO
SE
One class to another class type :
SE
Use one-argument constructor or conversion function depends upon the
defining conversion routine in source class or destination class.
DO
KA
DE
CO
Explain the use of scope resolution operator and
SE
memory management operators in C++ with examples.
DO
Scope resolution operator:
1. In C++, scope resolution operator (: :) is used to access a global variable
from a function in which a local variable is defined with the same name as a
KA
global variable.
2. For example:
In following program, the function main () access the global variable num
and also the local variable with the same name.
DE
CO
CO
DE
KA
DO
SE
Memory management operator:
SE
1. (1) C++ provides following two memory management operator
2. New ii. Delete
DO
3. (2) The new operator obtains memory block from operating
system and returns a pointer ot its starting point. The new
operator returns NULL, if memory allocation is unsuccessful.
KA
4. The general format of new operator is:
5. DataType *new DataType [size in integer];
DE
6. (3) The delete operator is used to return the memory allocated
by the new operator back ot the memory pool. Thus released
memory will be reused by other parts of the program.
CO
7. The general format of delete operator is : delete
pointervariable;
CO
DE
KA
DO
SE
SE
In above example, the new operator returns a pointer that point to
a memory section large enough to hold the string str plus an extra
DO
byte for null character.
Then after use of memory delete operator released memory.
KA
DE
CO
SE
!
DO
Thanks
KA
DE
Subscribe to Code Ka Dose channel for
CO