[go: up one dir, main page]

0% found this document useful (0 votes)
845 views15 pages

Q1: What Is Inheritance? Discuss Its Types

Inheritance allows classes to acquire properties of other classes. There are five types of inheritance: single, multiple, hierarchical, multilevel, and hybrid. Object-oriented programming organizes code around objects rather than actions/data. Its main features are objects, classes, abstraction, encapsulation, inheritance, polymorphism, overloading, and exception handling. Constructors initialize instances of a class and have the same name as the class. Types of constructors include default, parameterized, copy, and overloaded constructors. Structures allocate separate memory for each member while unions allocate space for the largest member only.

Uploaded by

Manikant SAh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
845 views15 pages

Q1: What Is Inheritance? Discuss Its Types

Inheritance allows classes to acquire properties of other classes. There are five types of inheritance: single, multiple, hierarchical, multilevel, and hybrid. Object-oriented programming organizes code around objects rather than actions/data. Its main features are objects, classes, abstraction, encapsulation, inheritance, polymorphism, overloading, and exception handling. Constructors initialize instances of a class and have the same name as the class. Types of constructors include default, parameterized, copy, and overloaded constructors. Structures allocate separate memory for each member while unions allocate space for the largest member only.

Uploaded by

Manikant SAh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Q1: What is Inheritance? Discuss its types.

Inheritance can be defined as the process where one class acquires the properties
(methods and fields) of another. The class which inherits the properties of other is
known as subclass (derived class, child class) and the class whose properties are
inherited is known as super class (base class, parent class).
A class derivation list names one or more base classes and has the form:
class derived-class: access-specifier base-class
Where access-specifier is one of public, protected, or private, and base-class is
the name of a previously defined class. If the access-specifier is not used, then it is
private by default.

Class derivedclass : access specifier baseclass


class Rectangle
{
... .. ...
};
class Area : public Rectangle
{
... .. ...
};
Types of Inheritance
There are five types of Inheritance.
1.
Single Inheritance
2.
Multiple Inheritance
3.
Hierarchical Inheritance
4.
Multilevel Inheritance
5.
Hybrid Inheritance (also known as Virtual Inheritance)
Single Inheritance
In this type of inheritance one derived class inherits from only one base class. It is
the simplest form of Inheritance.

Multiple Inheritances
In this type of inheritance a single derived class may inherit from two or more than
two base classes.

Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from a single base
class.

Multilevel Inheritance
In this type of inheritance the derived class inherits from a class, which in turn
inherits from some other class. The Super class for one, is sub class for the other.

Hybrid (Virtual) Inheritance


Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance.

Q2: What is Object Oriented Programming? Explain its features in detail.

Object-oriented programming (OOP) is a programming language model organized


around objects rather than "actions" and data rather than logic. Historically, a program has
been viewed as a logical procedure that takes input data, processes it, and produces output
data. The core of the pure object-oriented programming is to create an object, in code,
that has certain properties and methods. While designing C++ modules.

The main features of object oriented programming which you will be using in C++.
1.

Objects

2.

Classes

3.

Abstraction

4.

Encapsulation

5.

Inheritance

6.

Overloading

7.

Exception Handling

Objects
Objects are the basic unit of OOP. They are instances of class, which have data members and
uses various member functions to perform tasks.

Class
It is similar to structures in C language. Class can also be defined as user defined data type but
it also contains functions in it. So, class is basically a blueprint for object. It declare & defines
what data variables the object will have and what operations can be performed on the class's
object.

Abstraction
Abstraction refers to showing only the essential features of the application and hiding the
details. In C++, classes provide methods to the outside world to access & use the data
variables, but the variables are hidden from direct access.

Encapsulation
It can also be said data binding. Encapsulation is all about binding the data variables and
functions together in class.

Inheritance
Inheritance is a way to reuse once written code again and again. The class which is inherited is
called base calls & the class which inherits is called derived class. So when, a derived class
inherits a base class, the derived class can use all the functions which are defined in base class,
hence making code reusable.

Polymorphism
Polymorphism makes the code more readable. It is a features, which lets is create functions
with same name but different arguments, which will perform differently. That is function with
same name, functioning in different

Overloading
Overloading is a part of polymorphism. Where a function or operator is made & defined many
times, to perform different functions they are said to be overloaded.

Exception Handling
Exception handling is a feature of OOP, to handle unresolved exceptions or errors produced at
runtime.

Q3: What is Constructor? Discuss types of Constructor.


A constructor is a kind of member function that initializes an instance of its class.

A class constructor is a special member function of a class that is executed


whenever we create new objects of that class. A constructor will have exact same
name as the class and it does not have any return type at all, not even void.
Constructors can be very useful for setting initial values for certain member
variables.
Rules:
1. Constructor should be same name as the class and declared in the public
section of the class.
2. Constructors are invoked automatically whenever the object is created.
3. Constructors do not have return type.
4. Constructor can not be inherited, but from the derived class we can call the base
class constructors.
5. Constructor can not be virtual.
6. It is not possible to refers to the address of constructors.
7. It is not possible to use the constructor as member of union if the object is
created with constructor.
class A
{
int x;
public:
A(); //Constructor
};

Types of Constructor:
The various types of Constructor are as follows:-

1. Default Constructor:- Default Constructor is also called as Empty Constructor


which has no arguments and It is Automatically called when we creates the object
of class.
2. Parameterized Constructor: - This is another type Constructor which has
some Arguments and same name as class name but it uses some Arguments So For
this We have to create object of Class by passing some Arguments at the time of
creating object with the name of class.
3. Copy Constructor:- In this Constructor we pass the object of class into the
Another Object of Same Class. As name Suggests us Copy, means Copy the values
of one Object into the another Object of Class .This is used for Copying the values of
class object into another object of class.

4. Constructor Overloading: Constructor can be overloaded in


similar way as function overloading. Overloaded constructors
have same name(name of the class) but different number of
argument passed. Depending upon the number and type of
argument passed, specific constructor is called.
Q4: Difference between Structure and Union
1.

In structure each member get separate space


in memory.

2.
3.

Struct keyword is used.

1.

In union, the total memory space allocated is


equal to the member with largest size. All other

struct student
{
int a;
char b;
float c;
}s1;
4. The total memory required to store a structure
variable is equal to the sum of size of all the
members. In above case 7 bytes (2+1+4) will
be required to store structure variable s1.

members share the same memory space.

2. Union keyword is used.


3.

4.

union student
{
int a;
char b;
float c;
}s1;
In above example variable marks are of float
type and have largest size (4 bytes). So the

total memory required to store union variable s1

is 4 bytes.
5.
6.

It Consume more Memory

All Members can be initialized.


7. It has less Ambiguity
8. Self referential structure can be implemented
9.

Individal member can be accessed at a time

a
b
c
5. It Consume less Memory

6. Only first member can be initialized.


7. it has more Ambiguity
8. Self referential union can not be implemented
9.Only one member can be accessed at a time.

Q5: a) What is friend Function?


Or
Can we we access private member outside the class? If yes, how?
Friend function is used to access private members outside the class. We use friend keyword to declare it.

The declaration of friend function should be made inside the body of


class. We can make a function a friend to a class by declaring a prototype of this
external function within the class, and preceding it with the keyword friend

friend void functionname(classname objectname);


Friend function can not called with Dot(.) Operator.
#include<iostream.h>
#include<conio.h>
class

abc

{
Private:
int a;
public:
void m()
{
cout<<"Enter two values:";
cin>>a;
}
friend void ff(abc obj);
};
void ff(base obj)
{
Cout<<obj.a;
}
void main()
{
clrscr();
abc obj1l
obj1.m();
abc(obj1);
getch();
}

b) Inline Function
If a function is inline, the compiler places a copy of the code of that function at
each point where the function is called at compile time. Any change to an inline
function could require all parts of the function to be recompiled because compiler
would need to replace all the code once again otherwise it will continue with old
functionality. We have to use inline keyword to declare it.
#include <iostreaml.h>
#include<conio.h>

inline void hello()

{
cout<<"hello";
}
void main()
{
Void hello();
hello();

Q6: What is file handling?


Operations.

Discuss various file opening Modes and

File Handling concept in C++ language is used for store a data permanently in computer. Using file handling we can
store our data in Secondary memory (Hard disk).We use file handing for permanet storage and The transfer of input data or output - data from one computer to another can be easily done by using files.
For read and write from a file you need another standard C++ library called fstream, which defines three new data
types:
1. Ofstream: This is used to create a file and write data on files
2. ifstream: This is used to read data from files
3. fstream: This is used to both read and write data from/to files
File Opening mode
Mode
Meaning
Purpose
ios :: out
Write
Open the file for write only.
ios :: in
read
Open the file for read only.
ios :: app
Appending
Open the file for appending data to end-of-file.
ios :: ate
Appending
take us to the end of the file when it is opened.
Functions use in File Handling
Function
Operation
open()
To create a file
close()
To close an existing file
get()
Read a single character from a file
put()
write a single character in file.
read()
Read data from file
write()
Write data into file.

Q7: What is polymorphism? Discuss the example


Or
What is Virtual Function? Discuss early and late binding

Polymorphism is the ability of an object to take on many forms. The most common
use of polymorphism in OOP occurs when a parent class reference is used to refer
to a child class object.
Virtual Function: A virtual function is a member function that is declared within a
base class and redefined by a derived class. To create virtual function, precede the
functions declaration in the base class with the keyword virtual. When a class
containing virtual function is inherited, the derived class redefines the virtual
function to suit its own needs.
if we want to call derived class function using base class pointer, it can be achieved
by defining the function as virtual in base class.
Early Binding: which function is going to be called by object is decided by
compiler at compile time known as early binding.
Late Binding: which function is going to be called by object is decided at run time
using pointer object known as late binding. It is done by using pointer.
class Base
{
public:
virtual void show()
{
cout << "Base class";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class";
}
}
void main()
{
Base* b;
Derived d;
b = &d;
b->show();
}
Q8: What is function? Discuss call by value and call by reference.

A function is a group of statements that together perform a task. We can divide up


your code into separate functions. A function contains a unique name, return type
and arguments.
There are two types of functions
1. System Defined: Those are already defined in C++ Library as header files like
math, String, I/O functions.
2. User Defined: Those functions are created by users according to requirement
with an unique name.
Call by Value:
The call by value method of passing arguments to a function copies the actual
value of an argument into the formal parameter of the function. In this case,
changes made to the parameter inside the function have no effect on the
argument.
#include <iostream.h.h>
void swap(int x, int y);
void main ()
{
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
}
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
cout<<x<<y;
}
Call by Reference:
The call by reference method of passing arguments to a function copies the
reference of an argument into the formal parameter. Inside the function, the
reference is used to access the actual argument used in the call. This means that
changes made to the parameter affect the passed argument.
#include <iostream.h>
void swap(int &x, int &y);
void main ()
{

int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
}
void swap(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
cout<<x<<y;
}
Q9: Explain Conditional statements in details
The statements in the programs presented above have all been sequential, executed in the order they appear in the main
program.

C++ supports various types of conditional statements


a) Decision Making Statement:
Decision making structures require that the programmer specify one or more
conditions to be evaluated or tested by the program, along with a statement or
statements to be executed if the condition is determined to be true, and
optionally, other statements to be executed if the condition is determined to be
false.
Statement

Description

if statement

An if statement consists of a boolean expression followed by one or


more statements.
An if statement can be followed by an optional else statement, which
executes when the boolean expression is false.
A switch statement allows a variable to be tested for equality against a
list of values.
You can use one if or else if statement inside another if or else if
statement(s).
You can use one switch statement inside another switch statement(s).

if...else statement
switch statement
nested if statements
nested
statements

switch

b). Looping Statements:


There may be a situation, when you need to execute a block of code several
number of times. In general statements are executed sequentially: The first
statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more
complicated execution paths.

Loop Type
while loop
for loop
do...while loop
nested loops

Description
Repeats a statement or group of statements while a given condition is true.
It tests the condition before executing the loop body.
Execute a sequence of statements multiple times and abbreviates the code
that manages the loop variable.
Like a while statement, except that it tests the condition at the end of the
loop body
You can use one or more loop inside any another while, for or do..while
loop.

Q10: Discuss Operator Overloading in C++.

perator overloading is an important concept in C++. It is a type of


polymorphism in which an operator is overloaded to give user defined
meaning to it. Overloaded operator is used to perform operation on userdefined data type. For example '+' operator can be overloaded to perform
addition on various data types, like for Integer, String(concatenation) etc

Implementing Operator Overloading


Operator overloading can be done by implementing a function which can
be :
1. Member Function
2. Non-Member Function
3. Friend Function

Restrictions on Operator Overloading


Following are some restrictions to be kept in mind while implementing
operator overloading.

1. Precedence and Associatively of an operator cannot be changed.


2. Arity (numbers of Operands) cannot be changed. Unary operator
remains unary, binary remains binary etc.
3. No new operators can be created, only existing operators can be
overloaded.
4. Cannot redefine the meaning of a procedure. You cannot change how
integers are added.
Overloading Unary Operator
The unary operators operate on a single operand and following are the examples
of Unary operators:

The increment (++) and decrement (--) operators.


The unary minus (-) operator.
The logical not (!) operator.
Overloading Binary Operator
The binary operators take two arguments and following are the examples of Binary
operators. You use binary operators very frequently like addition (+) operator,
subtraction (-) operator and division (/) operator.
Following example explains how addition (+) operator can be overloaded. Similar
way, you can overload subtraction (-) and division (/) operators.

You might also like