Oop Agb
Oop Agb
1
OBJECT ORIENTED PROGRAMMING
2
OBJECT ORIENTED PROGRAMMING
Q:01| Draw the basic structure of a C++ program.
Include files
Class declaration
Q:02| Explain function prototyping with example. Write down the advantages of function
proto typing.
Ans: Function prototyping is one of the major improvements added to C++ functions. The
prototype describes the function interference to the compiler by giving details such as the
number and type of arguments and the type of return values.
1. It tells the return type of the data that the function will return.
2. It tells the number of arguments passed to the function.
3. It tells the data types of the each of the passed arguments.
4. Also it tells the order in which the arguments are passed to the function.
Ans: Control structures are the basic entities of a programming language. Control structures
are used to alter the flow of execution of the program.
1. Sequence structure.
2. Selection structure.
3
OBJECT ORIENTED PROGRAMMING
3. Loop structure.
-----------------
-----------------
If (x==100)
cout<<”x is 100”;
else
-----------------
-----------------
Ans: Input Operator: The input operator, commonly known as the extraction operator (>>), is
used with the standard input stream, cin. The input operator works on two operands, namely,
4
OBJECT ORIENTED PROGRAMMING
the c in stream on its left and a variable on its right. Thus, the input operator takes (extracts)
the value through cin and stores it in the variable.
Output Operator: The output operator, commonly known as the insertion operator (<<), is used
with The standard output stream cout. The output operator works on two operands, namely,
the cout stream on its left and the expression to be displayed on its right. The output operator
directs (inserts) the value to cout.
-----------------
-----------------
int a;
cin>>a;
a=a+1;
cout<<a;
-----------------
-----------------
i) Data hiding,
ii) Polymorphism
Ans: Data hiding: Data hiding is a software development technique specifically used in object-
oriented programming (OOP) to hide internal object details (data members). Data hiding ensures
exclusive data access to class members and protects object integrity by preventing unintended
or intended changes.
Data hiding also reduces system complexity for increased robustness by limiting
interdependencies between software components.
5
OBJECT ORIENTED PROGRAMMING
Polymorphism: The word polymorphism means having many forms. Typically, polymorphism
occurs when there is a hierarchy of classes and they are related by inheritance.
C++ polymorphism means that a call to a member function will cause a different function to be
executed depending on the type of object that invokes the function.
Ans: An inline function is a function that is expanded in line when it is invoked. That is, the
compiler replaces the function call with the corresponding function code (something similar to
macros expansion). The inline functions are a C++ enhancement feature to increase the execution
time of a program.
-----------------
-----------------
return (a*a*a);
#include <iostream>
class B;
class A {
private:
int numA;
6
OBJECT ORIENTED PROGRAMMING
public:
};
class B {
private:
int numB;
public:
B(): numB( 1) { }
};
int main()
A objectA;
B objectB;
return 0;
Q:08| What is inheritance? Explain the various types of inheritance. Explain multiple
inheritance.
7
OBJECT ORIENTED PROGRAMMING
Ans: The mechanism of driving a new class from an old one is called inheritance. The old class is
referred to as the base class and the new one is called the derived class or subclass.
1. A derived class with only one base class, is called single inheritance.
3. A traits of one base class may be inherited by more than one derived class, this process is
known as hierarchical inheritance.
4. The mechanism of deriving a class from another derived class is known as multilevel
inheritance.
Multiple inheritance: A derived class with several base classes, is called multiple inheritance.
Example:
-----------------
-----------------
class A
8
OBJECT ORIENTED PROGRAMMING
{
public:
};
class B
public:
};
public:
};
int main()
C c;
return 0;
Q:09| What is operator overloading? Write down the rules of operator overloading.
Ans: Operator overloading is function where different operators are applied and depends on the
arguments. Operator- ‘*’ can be used to pass through the function, and it has their own
precedence to execute.
9
OBJECT ORIENTED PROGRAMMING
1. Only built-in operators can be overloaded. New operators cannot be created.
2. Arity of the operators cannot be changed.
3. Precedence and associativity of the operators cannot be changed.
4. Overloaded operators cannot have default arguments except the function call operator ()
which can have default arguments.
5. Operators cannot be overloaded for built in types only. At least one operand must be
used defined type.
6. Assignment (=), subscript ([]), function call (“()”), and member selection (->) operators
must be defined as member functions.
7. Except the operators specified in point 6, all other operators can be either member
functions or a non-member functions.
8. Some operators like (assignment)=, (address)& and comma (,) are by default overloaded.
Ans: Data types in C++ can be classified under various categories as shown in bellow figure-
Q:11| Develop a C++ program to swap two variables using reference variable.
10
OBJECT ORIENTED PROGRAMMING
Ans:
#include <iostream>
int main()
temp = a; a = b;
b = temp;
return 0;
1) Friends should be used only for limited purpose. too many functions or external classes are
declared as friends of a class with protected or private data, it lessens the value of encapsulation
of separate classes in object-oriented programming.
11
OBJECT ORIENTED PROGRAMMING
Q:13| Explain friend class and friend function with example.
Ans: Friend Class: A friend class can access private and protected members of other class in
which it is declared as friend. It is sometimes useful to allow a particular class to access private
members of other class.
Example:
class Node
private:
int key;
Node *next;
};
Friend function: A friend function of a class is defined outside that class's scope but it has the
right to access all private and protected members of the class. Even though the prototypes for
friend functions appear in the class definition, friends are not member functions.
Example:
class Node
private:
int key;
Node *next;
};
12
OBJECT ORIENTED PROGRAMMING
Ans: Destructors have the opposite function of a constructor. The main use of destructors is to
release dynamic allocated memory. Destructors are used to free memory, release resources and
to perform other clean up. Destructors are automatically named when an object is destroyed.
Like constructors, destructors also take the same name as that of the class name.
Example:
-----------------
-----------------
class A{
public:
};
Ans: This is a special constructor for creating a new object as a copy of an existing object. There
will be always only one copy constructor that can be either defined by the user or the system.
Example:
-----------------
-----------------
person q (“Tropa”);
person r (p);
person p = q;
p = q;
-----------------
-----------------
13
OBJECT ORIENTED PROGRAMMING
Q:16| Write down the differences between static and dynamic binding.
Ans: The difference between static and dynamic binding is given bellow-
Actual object Actual object is not used for Actual object is used for binding.
binding.
Also known as It is also known as early binding It is also known as late binding
because binding happens during because binding happens at run
compilation. time.
Methods of Private, static and final methods Other than private, static and final
binding show static binding. Because, they methods show dynamic binding.
cannot be overridden. Because, they can be overridden.
Ans: Function overloading is a feature in C++ where two or more functions can have the same
name but different parameters.
Using the concept of function overloading; we can design a family of functions with one function
name but with different arguments lists.
Example:
-----------------
-----------------
int main ()
14
OBJECT ORIENTED PROGRAMMING
{
a = -5;
b = 5.5;
cout << Absolute value of" <<a<< "=" << absolute (a) << endl;
cout << Absolute value of" <<b<< "=" << absolute (b);
return 0;
if (var<0)
var = -var;
return var;
if (var<0.0)
var = -var;
return var;
Symbols Meanings
“=” Equal
15
OBJECT ORIENTED PROGRAMMING
“+=” Add Equal
• String
• Sub-string
• C-string
• Buffer
• Fill
• Range
• Initializer
Q:19| Define member function. Describe how member functions are declared inside the class
and outside the class.
Ans: Member functions are operators and functions that are declared as members of a class.
Member functions do not include operators and functions declared with the friend specifier.
class Box {
public:
double length;
double breadth;
double height;
16
OBJECT ORIENTED PROGRAMMING
double getVolume(void);
};
class Box {
public:
double length;
double breadth;
double height;
double getVolume(void) {
};
Q:20| What is virtual function? Write down the rules pf virtual function.
Ans: A virtual function is a special form of member function that is declared within a base class
and redefined by derived class. The keyword virtual is used to create a virtual function, precede
the function's declaration in the base class. If a class includes a virtual function and if it is
inherited, the virtual class redefines a virtual function to go with it's own need.
Example:
-----------------
-----------------
void SayHi()
17
OBJECT ORIENTED PROGRAMMING
{
• Virtual functions ensure that the correct function is called for an object, regardless of the
type of reference (or pointer) used for function call.
• They are mainly used to achieve Runtime polymorphism
• Functions are declared with a virtual keyword in base class.
Q:22| Define Input and output stream. Write the use of put(), write(), get line(), get(),
read().
Ans: Input stream: Input stream objects can read and interpret input from sequences of
characters. Specific members are provided to perform these input operations. The standard
object cin is an object of this type
Output stream: Output stream objects can write sequences of characters and represent other
kinds of data. Specific members are provided to perform these output operations. The standard
objects cout, cerr and clog are objects of this type.
18
OBJECT ORIENTED PROGRAMMING
put() function: put() function is used to write an unformatted character.
getline() function: getline() function is used to read a string or a line from input stream.
Ans: Object: Objects are the basic run-time entities in an object oriented system. They may
represent a person, a place, a bank account, a table of data or any item that the program has to
handle. Objects take up space in the memory and have an associated address.
For example, if "customer" and "account" are two objects in a program, then the customer object
may send a massage to the account object requesting for the bank balance.
Class: Once class has been defined, we can create any number of objects belonging to that class.
Each object is associated with the data of type class with which they are created. A class is thus
a collection of objects of similar types.
Example:
fruit mango;
19
OBJECT ORIENTED PROGRAMMING
Ans: An operator is a symbol that tells the computer to perform certain mathematical or logical
manipulations. C++ supports a rich set of built –in operators. Operators are used in programs to
manipulate data and variables. They usually from a part of mathematical or logical expression.
02. Relational operators. Ex- <, >, <=, >=, ==, !=.
04. Assignment operators. Ex- =, +=, -=, *=, /=, %=, ^=, &=.
05. Increment and decrement operators. Ex- ++i, i++, --i, i--.
Ans: The keywords implement specific C++ language features. They are explicitly reserved
identifiers and cannot be used as names for the program variables or other user-defined program
elements. Example: default, else, sizeof, void, namespace etc.
Ans: Data encapsulation led to the important OOP concept of data hiding. Data encapsulation is
a mechanism of bundling the data, and the functions that use them and data abstraction is a
mechanism of exposing only the interfaces and hiding the implementation details from the user.
Ans: Reference variable provides an alias (alternative name) for a previously defined variable.
Example:
20
OBJECT ORIENTED PROGRAMMING
float total = 100;
Ans: Binding means matching the function call with the correct function definition by the
compiler. It takes either at compile time or at runtime.
Ans: The scope resolution operator (::) is used to identify and disambiguate identifiers used in
different scopes.
• Real-time systems
• Object-oriented databases
• CIM/CAM/CAD systems
• Through inheritance, we can eliminate redundant code and extend the use of existing classes.
21
OBJECT ORIENTED PROGRAMMING
• We can build programs from the standard working modules that communicate with one
another, rather than having to start writing the code from scratch. This leads to saving of
development time and productivity.
• The principle of data hiding helps the programmer to build secure program that cannot be
invaded by code in other parts of the program.
C C++
7. Virtual and friend functions are not 7. Virtual and friend functions are supported.
supported.
8. main() function can be called from 8. main() function cannot be called from
functions. functions.
22
OBJECT ORIENTED PROGRAMMING
Q:32| Define Function call by value
Ans: Function 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.
Example:
-----------------
-----------------
int temp;
temp = x;
x = y;
y = temp;
return;
Q:33| What is the difference between operator overloading as a member function, using friend
function?
Ans: A member function is the one which is defined inside a class and is a member of the class.
It may either be a public private or protected function.
On other hand we use friend function in the case when we want to one class to communicate
with other class. for this we need to declare that friend function in both the classes and define
that friend function outside the associated classes. The friend function is always capable to access
all the members of the associated classes.
23
OBJECT ORIENTED PROGRAMMING
• Science C++ allows us to create hierarchy-related objects, we can build special object-oriented
libraries which can be used later by many programmers.
• While C++ is able to map the real-world properly, the C part of C++ gives the language the ability
to get close to the machine-level details.
• C++ programs are easily maintainable and expandable. When a new feature needs to be
implemented, it is very easy to add to the existing structure of an object.
• It is expected that C++ will replace C as a general-purpose language in the near future.
Ans: Tokens: As we know, the smallest individual units in a program is known as tokens. C++ has
the following tokens-
• Keywords
• Identifies
• Constants
• Strings
• Operators
24
OBJECT ORIENTED PROGRAMMING
Arguments Constructor accepts argument. Destructor does not accepts any
argument.
Ans: Explanation of different different types of file mood are given bellow-
• Opening a File: A file must be opened before you can read from it or write to it. Either
ofstream or fstream object may be used to open a file for writing. And ifstream object is
used to open a file for reading purpose only.
• Closing a File: When a C++ program terminates it automatically flushes all the streams,
release all the allocated memory and close all the opened files. But it is always a good
practice that a programmer should close all the opened files before program
termination.
• Writing to a File: While doing C++ programming, you write information to a file from
your program using the stream insertion operator (<<) just as you use that operator to
output information to the screen. The only difference is that you use an ofstream or
fstream object instead of the cout object.
25
OBJECT ORIENTED PROGRAMMING
• Reading from a File: You read information from a file into your program using the
stream extraction operator (>>) just as you use that operator to input information from
the keyboard. The only difference is that you use an ifstream or fstream object instead
of the cin object.
• File Position Pointers: Both istream and ostream provide member functions for
repositioning the file-position pointer. These member functions are seekg ("seek get")
for istream and seekp ("seek put") for ostream.
Ans: OOPS is abbreviated as Object Oriented Programming system in which programs are
considered as a collection of objects. Each object is nothing but an instance of a class.
1. Abstraction.
2. Encapsulation.
3. Inheritance.
Ans: Manipulators are the functions which can be used in conjunction with the insertion (≪) and
extraction (≫) operators on an object. Examples are endl and setw.
Ans: Constructor is a method use to initialize the state of an object, and it gets invoked at the
time of creation. Rules for constructor are-
26
OBJECT ORIENTED PROGRAMMING
Q:42| Define destructor.
Ans: The destructor is a member function whose name is the same as the class name but
preceded by a tilde. For example, the destructor for the class integer can be defined as shown
bellow-
~ integer(){ };
Ans: An abstract class is a class which cannot be instantiated. Creation of an object is not possible
with an abstract class, but it can be inherited. An abstract class can contain only abstract method.
Java allows only abstract method in abstract class while for the language it allows non-abstract
method as well.
Ans: There are three types of constructor, and they are as follows-
Ans: Binding is nothing but the association of a name with the class.
Static binding: Static binding is a binding in which name can be associated with the class during
compilation time, and it is also called as early binding.
Dynamic binding: Dynamic binding is a binding in which name can be associated with the class
execution time, and it is also called as late binding.
27
OBJECT ORIENTED PROGRAMMING
Programs
***A simple C++ program
#include <iostream>
int main()
return 0;
#include <iostream>
int main()
average = sum/2;
return 0;
28
OBJECT ORIENTED PROGRAMMING
}
#include <iostream>
class Box
private:
int length;
public:
Box(): length(0) { }
};
int printLength(Box b)
b.length += 10;
return b.length;
int main()
Box b;
return 0;
29
OBJECT ORIENTED PROGRAMMING
***Friend function with two class:
#include <iostream>
class B;
class A
public:
void showB(B& );
};
class B
private:
int b;
public:
B() { b = 0; }
};
int main()
A a;
B x;
a.showB(x);
30
OBJECT ORIENTED PROGRAMMING
return 0;
#include <iostream>
int main()
temp = a; a = b;
b = temp;
return 0;
***Member function:
#include<iostream>
class Test {
int value;
public:
31
OBJECT ORIENTED PROGRAMMING
Test(int v = 0) {value = v;}
};
int main() {
const Test t;
return 0;
***Inline function:
#include<iostream>
return (x > y)
int main() {
cout << “Max (20, 10): “ << Max (20, 10) << endl;
cout << “Max (0, 200): “ << Max (0, 200) << endl;
cout << “Max (100, 1010): “ << Max (100, 1010) << endl;
return 0;
***Inheritance:
#include<iostream>
32
OBJECT ORIENTED PROGRAMMING
using namespace std;
class A
public:
};
class B
public:
};
public:
};
int main()
C c;
return 0;
#include<iostream.h>
33
OBJECT ORIENTED PROGRAMMING
class student
private:
int roll;
public:
student ()
roll=0;
height=0;
weight=0;
void display ()
cout<<”\n Height”<<height;
cout<<”\n weight”<<weight;
~ student ()
34
OBJECT ORIENTED PROGRAMMING
};
void main ()
student obj;
return 0;
35