0% found this document useful (0 votes)
38 views20 pages

Nokol

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 20

Question: compare and contest between structure

programming language and opp paradigm? Do you


think oop paradigm is superior to structure
programming language ?

Ans : Structured programming is a programming


paradigm which divides the code into modules or
function, while OOP is a programming paradigm
based on the concept of objects, which contain data
in the form of fields known as attributes, and code in
the form of procedures known as methods.

Page :1
Page :2
Yes I thik opp paradigm is superior to structure
programming language. OOP is the most popular
programming paradigm because of its unique
advantages like the modularity of the code and the ability
to directly associate real-world business problems in
terms of code. Object-oriented programming offers a
sustainable way to write spaghetti code.
Question: How a C++ program can generally organized
using the concept of oop?
Ans :
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or
functions that perform operations on the data, while
object-oriented programming is about creating objects
that contain both data and functions.
C++ is a strongly typed language and fully object-
oriented, that is, classes are types, and inheritance and
polymorphism are both supported. The encapsulation
mechanism of classes enables to create abstract data
types, that is, a data abstraction and its operations is
implemented within the class.
Page : 3
Question: Explain Data hiding in OOP mentioning its
significance. How can it offer in C++?
Ans: Data hiding is an object-oriented programming (OOP)
technique specifically used to hide internal object details.
What is data hiding explain with suitable example?
Example. In this example, there is a class with a variable and
two functions. Here, the variable "num" is private so, it can be
accessed only by the members of the same class, and it can't be
accessed anywhere else. Hence, it is unable to access this
variable outside the class, which is called data hiding.

Question: Explain what is abstract data type ? How


ADTS implemented in C++?
Ans : Abstract Data type (ADT) is a type (or class) for objects
whose behavior is defined by a set of values and a set of
operations.
An abstract data type (or ADT) is a class that has a defined set
of operations and values. In other words, you can create the
starter motor as an entire abstract data type, protecting all of the
inner code from the use. How is implemented in C++?

Page:4
Question: Mention four keys difference between C++
classes and structure.
Ans:
Class Structures
Class member are private by Structure member are public by
default. default.
Class is define to use keyword Structure is define to use
class. keyword struct.
Class is used in large program. Structure are used in small
program.
Class can be inherited But structure can’t be inherited.
Class occupied more space. Struct occupied less space
Class is a collection of data Structure is a collection of data
member and member function variable (member).
It provide high security It doesn’t provide any private
security.

Page - 5
Question: What is template in C++? Discuss the types of
template.
Ans:

There are three kinds of templates: class templates,


function templates and variable templates.
Class tamplate: The relationship between a class template
and an individual class is like the relationship between a
class and an individual object. An individual class defines
how a group of objects can be constructed, while a class
template defines how a group of classes can be generated.

Page- 6
Page_7
Function tamplate: Function templates are special
functions that can operate with generic types. This allows
us to create a function template whose functionality can
be adapted to more than one type or class without
repeating the entire code for each type. In C++ this can be
achieved using template parameters.
Variable tamplate: A variable template defines a family
of variable (when declared at namespace scope) or a
family of static data members (when defined at class
scope).
Question: How memory is allocated for the states and
behaviors of the object of a class ? Illustrate with an
example.
Ans: The way memory is allocated to variables and
functions of the class is different even though they both
are from the same class. The memory is only allocated to
the variables of the class when the object is created. The
memory is not allocated to the variables when the class is
declared.

Question: what is pointer?


Ans: A pointer is variable whose value is address of
another variable.

Page 7
Question: write a code segment in c++ to illustrate the
benefit of using pointer?

Ans:
The benefit of using pointer are :
1)Pointers save the memory.
2)Pointers reduce the length and complexity of a program.
3)Pointers allow the passing of arrays and strings to 4)function more
efficiently.
5)Pointers make it possible to return more than one value from the
function.
6)Pointers increase the processing speed.
Page 8
Question: what is different between concreat and
abstract class?
Ans : Abstract classes usually have partial or no
implementation. On the other hand, concrete classes
always have full implementation of its behavior.
Question: what problems may occurs in inheritance?
when should you declare a class as virtual ?
Ans:
In multiple inheritances, when one class is derived from two or
more base classes then there may be a possibility that the base
classes have functions with the same name, and the derived class
may not have functions with that name as those of its base
classes.
Virtual base classes are used in virtual inheritance in a way of
preventing multiple “instances” of a given class appearing in an
inheritance hierarchy when using multiple inheritances.
Question: why is inheritance called is a or is a kind of
relationship? explain with examples.
Ans: An inheritance relationship is an essential feature that
distinguishes object-oriented languages from conventional ones.
From the domain view, an inheritance relationship organizes
classes in hierarchical structures. This allows us to model a
hierarchy of terms using generalizations and specializations.
Page 9
Question: clearly write the rules for acquiring the properties by
the derived class when a basic class is inherited using the
visibility mode as private, public and protected.
Ans:
When a base class is derived by a derived class with the help of
inheritance, the accessibility of base class by the derived class is
controlled by visibility modes. The derived class doesn’t inherit
access to private data members. However, it does inherit a full
parent object, which contains any private members which that
class declares.
1. Public Visibility mode: If we derive a subclass from a
public base class. Then the public member of the base class
will become public in the derived class and protected
members of the base class will become protected in the
derived class.
2. Protected Visibility mode: If we derive a subclass from a
Protected base class. Then both public member and
protected members of the base class will become protected
in the derived class.
3. Private Visibility mode: If we derive a subclass from a
Private base class. Then both public member and protected
members of the base class will become Private in the
derived class.

Page 10
Question: discuss inline function default argument and friend
function in c++ with example.
Ans: Inline function: Inline function is an important feature that is
commonly used with classes. 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. Inline function decreases execution time of a
program and makes program efficient.
Default arguments: In C++, we can provide default values for
function parameters. A default argument is a value provided in
function declaration that is automatically assigned by the compiler if
caller function does not provide a value for the arguments.
Example:
int sum (int x, int y, int w = 0, int z = 0)

return (x + y + w + z);

int main()

cout << sum (10, 15) << endl << sum(10, 15, 20) << endl <<sum(10,
15, 20, 25) << endl;

Define friend function:


Private data members cannot be accessed from outside the class.
However situations arise where two classes need to share a particular
function for such situation C++ introduces friend function. These are
functions that can be made friendly with both classes.

Page 11
Question: what problems may occurs in inheritance in
c++?when should you declare a class as virtual ?
Ans:
In multiple inheritances, when one class is derived from two or
more base classes then there may be a possibility that the base
classes have functions with the same name, and the derived class
may not have functions with that name as those of its base
classes.
a virtual base class in C++ is used to avoid multiple "instances"
of a given class from occurring in an inheritance hierarchy.
Now, in this article, you will explore the virtual base class in
C++ as a whole. Consider the case where there is just one classA

Page 12
Question: mention the rules of using construction in inheirtance.
Ans: The main job of the constructor is to allocate memory for
class objects. Constructor is automatically called when the
object is created. Multiple Inheritance: Multiple Inheritance is a
feature of C++ where a class can derive from several(two or
more) base classes.
Question : Mention the drawbacks of structure
programming paradigm ?
Ans: Structures don't support data hiding. The members of
a structure can be accessed by any function regardless of its
scope. Static members cannot be declared inside the
structure body. Constructors cannot be created inside a
structure.

Question: how can the drawback can be solved by


oop?
Ans : Data hiding helps computer programmers create
classes with unique data sets and functions by avoiding
unnecessary entrance from other classes in the
program. Being a software development technique in
OOP, it ensures exclusive data access and prevents
intended or unintended changes in the data

Page “ 13
Question: What are access specifier in C++?
Ans : In C++, there are three access specifiers: public -
members are accessible from outside the class. private -
members cannot be accessed (or viewed) from outside the
class. protected - members cannot be accessed from
outside the class, however, they can be accessed in
inherited classes.
Question : Discuss the feature of OPP?
Ans : here are four fundamental concepts of Object-
oriented programming – Inheritance, Encapsulation,
Polymorphism, and Data abstraction.

Page : 14
h

Question : What are the main purpose of using friend


fuction?
Ans: A friend function is used to access all the non-
public members of a class. You can use a friend function
to bridge two classes by operating objects of two
different classes. It increases the versatility of
overloading operators.

Page : 15
Question : Define the scope resolution operator and
function overloading?
Ans : : (scope resolution) operator is used to get
hidden names due to variable scopes so that you can
still use them.

C++ lets you specify more than one function of the same name in the
same scope. These functions are called overloaded functions, or
overloads. Overloaded functions enable you to supply different
semantics for a function, depending on the types and
number of its arguments
Page :16
Question : write a program to illustrate the concept of class and object?
Ans : #include <iostream>
using namespace std;
class Phone {
public:
double cost;
int slots;
};
int main() {
Phone Y6;
Phone Y7;

Y6.cost = 100.0;
Y6.slots = 2;

Y7.cost = 200.0;
Y7.slots = 2;
cout << "Cost of Huawei Y6 : " << Y6.cost << endl;
cout << "Cost of Huawei Y7 : " << Y7.cost << endl;

cout << "Number of card slots for Huawei Y6 : " << Y6.slots << endl;
cout << "Number of card slots for Huawei Y7 : " << Y7.slots << endl;

return 0;
} (page 17)
Question : discuss inline function and default
argument.
Ans: inline function: The inline function is written like a normal
function in the source file but compiles into inline code instead of into a
function.
Calling a function generally causes a certain overhead (stacking
arguments, jumps, etc…), and thus for very short functions, it may be
more efficient to simply insert the code of the function where it is
called, instead of performing the process of formally calling a function.
When a function is declared with inline specifier, the function body is
actually inserted into the program wherever a function call occurs
during compile time. Also, the source file remains well organized and
easy to read, since the function is shown as a separate entity.
Declaring a function as inline does not change at all the behavior of a
function, but is merely used to suggest the compiler that the code
generated by the function body shall be inserted at each point the
function is called, instead of being invoked with a regular function call.
Defalt argument: In C++, functions can also have optional
parameters for which no arguments are required in the call. In
other words, a function can be called without specifying all its
arguments. For example, a function with three parameters may be
called with only two.

For this, the function shall include a default value for its last
parameter, which is used by the function when called with fewer
arguments.

Page 18
Question: how can you pass object to member
function?
Ans: To pass an object as an argument we write the object
name as the argument while calling the function the same
way we do it for other variables. Syntax:
function_name(object_name); Example: In this Example
there is a class which has an integer variable 'a' and a
function 'add' which takes an object as argument.
Question: disscuss constructor and copy constructor?
Ans: A copy constructor is a member function that
initializes an object using another object of the same
class. In simple terms, a constructor which creates an
object by initializing it with an object of the same class,
which has been created previously is known as a copy
constructor.
Page 19

You might also like