Oops Short Answer Questions
Oops Short Answer Questions
Short answer
1. Actually this section can be considered as sub section for the global declaration
section.
2. Class declaration and all methods of that class are defined here.
Section 4 : Main Function
1. Each and every C++ program always starts with main function.
2. This is entry point for all the function. Each and every method is called indirectly
through main.
3. We can create class objects in the main.
4. Operating system call this function automatically.
Each individual word and punctuation is referred to as a token in C++. Tokens are the
smallest building block or smallest unit of a C++ program.
These following tokens are available in C++:
Identifiers
Keywords
Constants
Operators
Strings
C++
constants
Floating
Integer Boolean Character String
Point
Declaring Constants:
Integer literals:
An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies
the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
An integer literal can also have a suffix that is a combination of U and L, for unsigned
and long, respectively. The suffix can be uppercase or lowercase and can be in any
order.
30 // int
30u // unsigned int
30l // long
30ul // unsigned long
Floating-point literals:
A floating-point literal has an integer part, a decimal point, a fractional part, and an
exponent part. You can represent floating point literals either in decimal form or
exponential form.
3.14159 // Legal
314159E-5L // Legal
Boolean literals:
There are two Boolean literals and they are part of standard C++ keywords:
You should not consider the value of true equal to 1 and value of false equal to 0.
Character literals:
Character literals are enclosed in single quotes. If the literal begins with L (uppercase
only), it is a wide character literal (e.g., L'x') and should be stored in wchar_t type of
variable . Otherwise, it is a narrow character literal (e.g., 'x') and can be stored in a
simple variable of char type.
A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a
universal character (e.g., '\u02C0').
String literals:
String literals are enclosed in double quotes. A string contains characters that are similar to
character literals: plain characters, escape sequences, and universal characters.
Various data items with symbolic names in C++ is called as Identifiers. Following data
items are called as Identifier in C++ –
1. Names of functions
2. Names of arrays
3. Names of variables
4. Names of classes
1. C++ is case-sensitive so that Uppercase Letters and Lower Case letters are
different
2. The name of identifier cannot begin with a digit. However, Underscore can be
used as first character while declaring the identifier.
3. Only alphabetic characters, digits and underscore (_) are permitted in C++
language for declaring identifier.
4. Other special characters are not allowed for naming a variable / identifier
5. Keywords cannot be used as Identifier.
Some valid examples:- sum, a1, a2, _1, _a, average, a_b, x123y...
Some invalid examples:- 1a, a-b, float
5. What is string in C++ ? How to define it in c++
In C programming, the collection of characters is stored in the form of arrays, this is
also supported in C++ programming. Hence it's called C-strings.
Defining a C-string?
char str[] = "C++";
Although, "C++" has 3 character, the null character \0 is added to the end of the string
automatically.
return-type : suggests what the function will return. It can be int, char, some
pointer or even a class object. There can be functions which does not return
anything, they are mentioned with void.
Function Name : is the name of the function, using the function name it is called.
Function body : is he part where the code statements are written.
main() function is the entry point of any C++ program. It is the point at which execution of
program is started. When a C++ program is executed, the execution control goes directly to
Syntax
void main()
{
............
............
}
In above syntax;
8. what is function call? write in how many ways the function call can be initiated.
Function call statement calls the function by matching its name and arguments. A
function call can be made by using function name and providing the required
parameters.
For example,
display(a);
s = sum(x,y);
A function can be called by two ways. They are:
Call by value
Call by reference
9. What are called as Inline Functions? write its syntax
Inline functions are actual functions, which are copied everywhere during compilation,
like preprocessor macro, so the overhead of function calling is reduced. All the functions
defined inside class definition are by default inline, but you can also make any non-class
function inline by using keyword inline with them.
For an inline function, declaration and definition must be done together. For example,
inline void fun(int a)
{
return a++;
}
Some Important points about Inline Functions
1. We must keep inline functions small, small inline functions have better efficiency.
2. Inline functions do increase efficiency, but we should not make all the functions
inline. Because if we make large functions inline, it may lead to code bloat, and
might affect the speed too.
3. Hence, it is adviced to define large functions outside the class definition using
scope resolution :: operator, because if we define such functions inside class
definition, then they become inline automatically.
4. Inline functions are kept in the Symbol Table by the compiler, and all the call for
such functions is taken care at compile time.
If a function is defined as a friend function in C++ then the protected and private data of
a class can be accessed using the function.
For accessing the data, the declaration of a friend function should be done inside the
body of a class starting with the keyword friend.
Unit 2
Short Answer
We can think of class as a sketch (prototype) of a house. It contains all the details
about the floors, doors, windows etc. Based on these descriptions we build the
house. House is the object.
As, many houses can be made from the same description, we can create many
objects from a class.
class className
{
// some data
// some functions
};
class Test
{
private:
int data1;
float data2;
public:
void function1()
{ data1 = 2; }
float function2()
{
data2 = 3.5;
return data2;
}
};
int main()
{
Test o1, o2;
}
Local class is a class defined inside a function. Following are some of the rules for using
these classes.
Global variables declared above the function can be used with the scope operator
"::".
Static variables declared inside the function can also be used.
Automatic local variables cannot be used.
It cannot have static data member objects.
Member functions must be defined inside the local classes.
Enclosing functions cannot access the private member objects of a local class.
1. Default Constructor
2. Parametrized Constructor
3. Copy COnstructor
Default Constructor
Default constructor is the constructor which doesn't take any argument. It has no
parameter.
Parameterised Constructors
These are the constructors with parameter. Using this Constructor you can provide
different values to data members of different objects, by passing the appropriate values
as argument.
COPY Constructor
scope operator - ::
sizeof
member selector - .
member pointer selector - *
ternary operator - ?:
Syntax:
Unit 3
Short answer
The process of obtaining the data members and methods from one class to another
class is known as inheritance. It is one of the fundamental features of object-oriented
programming.
Important points
In the inheritance the class which is give data members and methods is
known as base or super or parent class.
The class which is taking the data members and methods is known as sub or
derived or child class.
Syntax
class subclass_name : superclass_name
{
// data members
// methods
}
ambiguity can arise when several paths exist to a class from the same base class.
This means that a child class could have duplicate sets of members inherited from a
single base class.
C++ solves this issue by introducing a virtual base class. When a class is made
virtual, necessary care is taken so that the duplication is avoided regardless of the
number of paths that exist to the child class.
When two or more objects are derived from a common base class, we can prevent
multiple copies of the base class being present in an object derived from those
objects by declaring the base class as virtual when it is being inherited. Such a base
class is known as virtual base class. This can be achieved by preceding the base class’
name with the word virtual.
Abstract Class is a class which contains atleast one Pure Virtual function in it.
Abstract classes are used to provide an Interface for its sub classes. Classes
inheriting an Abstract Class must provide definition to the pure virtual function,
otherwise they will also become abstract class.
Characteristics of Abstract Class
Abstract class cannot be instantiated, but pointers and refrences of Abstract class
type can be created.
Abstract class can have normal functions and variables along with a pure virtual
function.
Abstract classes are mainly used for Upcasting, so that its derived classes can use its
interface. Classes inheriting an Abstract Class must implement all pure virtual
functions, or else they will become Abstract too.
In Late Binding function call is resolved at runtime. Hence, now compiler determines
the type of object at runtime, and then binds the function call. Late Binding is also
called Dynamic Binding or RuntimeBinding.
Mechanism of Late Binding
To accomplich late binding, Compiler creates VTABLEs, for each class with virtual
function. The address of virtual functions is inserted into these tables. Whenever an
object of such class is created the compiler secretly inserts a pointer called vpointer,
pointing to VTABLE for that object. Hence when function is called, compiler is able to
resovle the call by binding the correct function using the vpointer.
A virtual function whose declaration ends with =0 is called a pure virtual function.
For example,
class Weapon
{
public:
virtual void features() = 0;
};
Here, the pure virtual function is
virtual void features() = 0
And, the class Weapon is an abstract class.
All these classes are declared in the header file iostream.h. The file iostream.h must
be included in the program, if we are using the functions of these classes.
Unit 4
Short answer
There is a single definition of each container, such as vector, but we can define
many different kinds of vectors for example, vector <int> or vector <string>.
The concept of templates can be used in two different ways:
Function Templates
Class Templates
Like function templates, you can also create class templates for generic class
operations.
Sometimes, you need a class implementation that is same for all classes, only the
data types used are different.
Normally, you would need to create a different class for each data type OR create
different member variables and functions within a single class.
This will unnecessarily bloat your code base and will be hard to maintain, as a
change is one class/function should be performed on all classes/functions.
However, class templates make it easy to reuse the same code for all data types.
To create a class template object, you need to define the data type inside a < > when
creation.
className<dataType> classObject;
For example:
className<int> classObject;
className<float> classObject;
className<string> classObject;
A single function template can work with different data types at once but, a single
normal function can only work with one set of data types.
Normally, if you need to perform identical operations on two or more types of data,
you use function overloading to create two functions with the required function
declaration.
However, a better approach would be to use function templates because you can
perform the same task writing less and maintainable code.
int i = 2, j = 3;
cout << max(i, j);
string a(''Hello''), b(''World'');
cout<< max(a, b);
In this case, the compiler creates two different max()functions using the function
template
6. What is exception?
An exception is a problem that arises during the execution of a program. A C++
exception is a response to an exceptional circumstance that arises while a program
is running, such as an attempt to divide by zero.
Functions/Methods can handle any exceptions they choose: A function can throw
many exceptions, but may choose to handle some of them. The other exceptions
which are thrown, but not caught can be handled by caller. If the caller chooses not
to catch them, then the exceptions are handled by caller of the caller.
In C++, a function can specify the exceptions that it throws using the throw keyword.
The caller of this function must handle the exception in some way .
Grouping of Error Types: In C++, both basic types and objects can be thrown as
exception. We can create a hierarchy of exception objects, group exceptions in
namespaces or classes, categorize them according to types.
Exceptions provide a way to transfer control from one part of a program to another.
C++ exception handling is built upon three keywords: try, catch, and throw.
throw: A program throws an exception when a problem shows up. This is done
using a throw keyword.
throw: A program throws an exception when a problem shows up. This is done
using a throw keyword.
try: A try block identifies a block of code for which particular exceptions will be activated.
It's followed by one or more catch blocks.
The C++ STL (Standard Template Library) is a powerful set of C++ template classes
to provides general-purpose templatized classes and functions that implement
many popular and commonly used algorithms and data structures like vectors, lists,
queues, and stacks.
At the core of the C++ Standard Template Library are following three well-
structured components:
Component Description