oops qb sem 3
oops qb sem 3
DEPARTMENT OF CSBS
23CB1301-OBJECT ORIENTED PROGRAMMING
UNIT-I: INTRODUCTION TO C
PART A
1. Define identifier in C. [Remember]
In C programming, an identifier is a name used to identify a variable, function, array,
structure, union, label, or any other user-defined item. Identifiers are fundamental to the
program as they provide a way to refer to data and functions in the code.
● variable
● sum1
● _count
● TotalAmount
● data_array
Examples of Keywords in C
Used to define constant values that can be used throughout the program.
2. const Keyword:
Used to define constant variables whose values cannot be changed after initialization.
13marks:
"//print Hello World to the screen" is a single-line comment which is ignored by the compiler.
The comment line explains to the user that the 'printf()' function is used to print 'Hello world.
A single-line comment in C is used to add explanatory text or notes within the code. It starts
with // and extends to the end of the line. Single-line comments are ignored by the compiler
and do not affect the program's execution.
Example: #include <stdio.h> int main()
{ // This is a single-line comment
printf("Hello, world!\n"); // This line prints "Hello, world!" return 0; }
1. Single-line comments: Single-line comments start with // and extend to the end of the
line. They are used to add explanatory text or notes within the code.
Example:
#include <iostream>
std::cout << "Hello, world!" << std::endl; // This line prints "Hello, world!" return 0; }
2. Multi-line comments: Multi-line comments start with /* and end with */. They can
span across multiple lines and are used to comment out blocks of code or add longer
explanations.
{ /* This is a multi-line comment It can span across multiple lines and is useful for
commenting out blocks of code */
std::cout << "Hello, world!" << std::endl; // This line prints "Hello, world!" return 0; }
The scope of a variable in C++ refers to where in the program it can be accessed. It can be
local, limited to a specific block or function; class, accessible within a class definition; or
global, available throughout the entire program.
SYNTAX:
We can also declare the local variables in the header of a “for” statement. In this case it is
executed in the same manner as if it were part of a local variable declaration statement. For
example: for(int i=0;i<=5;i++){……} In above example int i=0 is a local variable
declaration.
In C++, there are five basic data types: int, float, char, bool, and double. Integers, floating-
point numbers, characters, Boolean values, & double-precision floating-point numbers are
each represented by one of these data types
10.What are the four types of User-defined Function Types in C++. [Understand]
● Function with no argument and no return value.
● Function with no argument but return value.
● Function with argument but no return value.
● Function with argument and return value.
Overloading is about the same function having different signatures. Overriding is about the
same function, and same signature but different classes connected through inheritance.
Overloading is an example of compiler time polymorphism and overriding is an example of
run time polymorphism.
A function is a block of statements that together performs a specific task by taking some
input and producing a particular output. Function overriding in C++ is termed as the
redefinition of base class function in its derived class with the same signature i.e. return type
and parameters.
What is the difference between function overloading and function overriding with an
example?
13.what is the main difference between function overloading and function overridding
in C++. [Understand]
The main difference between overloading and overriding is that overloading occurs when
methods in the same class have the same method name but different parameters, whereas
overriding occurs when two methods have the same method name and parameters.
Pass-by-reference: When a method is called, the method arguments reference the same
variable in memory as the caller. Pass-by-value: When a method is called, the caller passes a
copy of the argument variables to the method resulting in two values in memory.12 Jul 2022
24.How to call a pointer in C++? [Apply]
Passing the variable address from the calling function and using them as a pointer inside the
function is called the call-by-pointer. This method allows clearer visibility of functions in
which the value of the passed variables may change.
PART B
1. Explain in detail about the function overloading in c++ with examples. . [Understand]
2.Explain in detail about the new and Delete operator with its examples. [Understand]
3.Explain in detail about the inline function in C++ with its examples. [Understand]
PART C
1.Difference between the Passing by value and passing by reference in C++. [Apply]
2.Difference between the Function overloading and function overriding with its examples.
[Apply]
UNIT-3 :OOPs Concepts
PART A
1.What is the essentials for OOPs? [Understand]
There are four fundamental concepts of Object-oriented programming – Inheritance,
Encapsulation, Polymorphism, and Data abstraction.
9.What are two differences between data hiding and encapsulation? [Understand]
Two differences between Data Hiding and Encapsulation are as follows: Data hiding focuses
more on data security whereas, encapsulation focuses more on hiding the complexity of the
system. Data hiding focuses on restricting the use of data whereas, encapsulation deals with
wrapping of data and functions.
Procedural abstraction is the process of breaking down a complex problem into smaller, more
manageable steps. By identifying the essential actions that need to be taken in order to solve a
problem, procedural abstraction can help to make the problem-solving process more efficient
and effective.
14.What are the difference between classes and objects in C++? [Understand]
In C++, a member function is a function that is associated with a specific class or object.
These functions are defined within the class definition and provide the ability to perform
operations on the class's data members and interact with other objects of the same class.
There are 5 types of member functions that are generally provided in C++. Namely, simple,
static, const, inline and friend member functions. Member functions are specific to classes.
● Access modifiers (or access specifiers) are keywords in object-oriented languages that set the
accessibility of classes, methods, and other members. Access modifiers are a specific part of
programming language syntax used to facilitate the encapsulation of components.
Friend classes are those classes that have permission to access private members of the class in
which they are declared. The main thing to note here is that if the class is made friend of
another class then it can access all the private members of that class.
Errors indicate unrecoverable system issues beyond program control. In contrast, exceptions
represent unexpected events within the program that can often be handled gracefully.
PART-B
PART-C
1.Explain in detail about the Member function of class with its examples. [Understand]
2.Explain in detail about the error handling in C++ with its examples. [Understand]
UNIT-IV: INHERITANCE AND POLYMORPHISM
PART A
It allows us to create a new class (derived class) from an existing class (base class). The
derived class inherits the features from the base class and can have additional features of its
own. For example, class Animal { // eat() function // sleep() function }; class Dog : public
Animal { // bark() function };
Inheritance in C++ refers to the process wherein new classes can inherit properties (objects
and methods) from pre-existing classes. There are multiple benefits to this, like inducing code
reusability, modularity, easier maintenance, and more.
The Basic Syntax for defining the child class and parent class in all types of inheritance in
C++ is as follows: class parent_class{ //class definition of the parent class }; class child_class
: visibility_modeparent_class { //class definition of the child class };
A real-time example of multiple inheritance is a class that inherits from both a "Vehicle"
class and an "Electricity-Powered" class, to create a new class called "Electric Vehicle". This
class would have access to both the properties and methods of its parent classes.
11.What is an example of inheritance?
Inheritance is a way of representing real-world relationships between the two. Here's an
example – car, bus, bike – all of these come under a broader category called Vehicle. That
means they've inherited the properties of class vehicles, i.e., all are used for transportation.
12.What is class hierarchy with an example? [Understand]
A class hierarchy or inheritance tree in computer science is a classification of object types,
denoting objects as the instantiations of classes (class is like a blueprint, the object is what is
built from that blueprint) inter-relating the various classes by relationships such as "inherits",
"extends", "is an ...
13.What is an example of hierarchical inheritance? [Understand]
In hierarchical inheritance, all features that are common in child classes are included in the
base class. For example, Physics, Chemistry, Biology are derived from Science class.
Similarly, Dog, Cat, Horse are derived from Animal class.
The syntax of declaring a pointer is to place a * in front of the name. A pointer is associated
with a type (such as int and double ) too.
18.What are examples of objects in C++?[Understand]
C++ Object
In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc. In other
words, object is an entity that has state and behavior. Here, state means data and behavior
means functionality. Object is a runtime entity, it is created at runtime.
19.How to initialize a pointer to an object in C++?[Understand]
You need to initialize a pointer by assigning it a valid address. This is normally done via the
address-of operator ( & ). The address-of operator ( & ) operates on a variable, and returns
the address of the variable. For example, if number is an int variable, &number returns the
address of the variable number .
20.What is the syntax for declaring pointer to object? [Understand]
The syntax of declaring a pointer is to place a * in front of the name. A pointer is associated
with a type (such as int and double) too. Naming Convention of Pointers: Include a "p" or
"ptr" as prefix or suffix, e.g., iPtr, numberPtr, pNumber, pStudent.
21.What is an object explain with an example in C++?[Understand]
In C++, an object is an instance of a class that encapsulates data and functionality pertaining
to that data. Suppose a class named MyClass was created, so now it can be used to create
objects. To create an object of MyClass , specify the class name, followed by the object
name.
22.Can we assign one object to another object in C++?[Understand]
In C++, you can assign one object to another using the assignment operator ( = ) if the objects
have the same data type.
23.How to assign a class to another class in C++?[Understand]
Through class conversion, one can assign data that belongs to a particular class type to an
object that belongs to another class type. where '=' has been overloaded for objects of class
type 'B'. Class conversion can be achieved by conversion function which is done by the use of
operator overloading.
24.What is dynamic polymorphism in C++ with an example? [Understand]
Run-time polymorphism, also known as dynamic polymorphism, is achieved through
inheritance and virtual functions. Inheritance is the process of creating a new class from an
existing class, which inherits all the data members and member functions of the base class.
25.What is dynamic binding in C++ with an example? [Understand]
Dynamic binding in C++ is a practice of connecting the function calls with the function
definitions by avoiding the issues with static binding, which occurred at build time. Because
dynamic binding is flexible, it avoids the drawbacks of static binding, which connected the
function call and definition at build time.
26.What is a real life example of polymorphism in C++?[Understand]
Polymorphism in C++
Real life example of polymorphism, a person at the same time can have different
characteristic. Like a man at the same time is a father, a husband, an employee. So the same
person posses different behavior in different situations. This is called polymorphism.
27.What is static and dynamic binding polymorphism in C++?[Understand]
What is the difference between static binding and dynamic binding in C++? In static binding,
the function is bound at the compile time, whereas in dynamic binding, the function is bound
at run time. Static binding is also known as early binding or compile-time polymorphism.
28.What is a virtual function in C++ with example? [Understand]
A virtual function is a member function in the base class that we expect to redefine in derived
classes. For example, class Base { public: void print() { // code } }; class Derived : public
Base { public: void print() { // code } }; The print() method in the Derived class shadows the
print() method in the Base class.
29.What is a virtual base class in C++ with an example? [Understand]
Virtual Class is defined by writing a keyword “virtual” in the derived classes, allowing only
one copy of data to be copied to Class B and Class C (referring to the above example). It
prevents multiple instances of a class appearing as a parent class in the inheritance hierarchy
when multiple inheritances are used.
30.What is a virtual class in OOP? [Understand]
In object-oriented programming, a virtual base class is a nested inner class whose functions
and member variables can be overridden and redefined by subclasses of an outer class.
Virtual classes are analogous to virtual functions.
PART-B
1.Explain in detail about the operator overloading in C++ with its examples.[Understand]
2.Explain in detail about the types of inheritance and with its examples.[Understand]
3.Explain in detail about the Error handling in C++.[Understand]
PART-B
1.Explain in detail about the ponters to objects in C++ with its examples.[Understand]
2.Difference between the Overloading and Overridding in C++ with its
examples.[Understand]
UNIT-V
GENERIC PROGRAMMING AND I/O STREAMS:
PART-A
A template is a simple yet very powerful tool in C++. The simple idea is to pass the data type
as a parameter so that we don't need to write the same code for different data types. For
example, a software company may need to sort() for different data types.
9.What are class templates also called? [Understand]
Generic programming is an approach where generic data types are used as parameters in
algorithms so that they work for variety of suitable data types. Templates are sometimes
called parameterized classes or functions.
10.What is a function in C++ with an example? [Understand]
A function definition provides the actual body of the function. The C++ standard library
provides numerous built-in functions that your program can call. For example, function
strcat() to concatenate two strings, function memcpy() to copy one memory location to
another location and many more functions.
11.What is a function template in class? [Understand]
A class template definition must declare the class data and function members, as in the
following examples. template <class Elem> class Array { Elem* data; int size; public: Array(
intsz ); intGetSize(); Elem& operator[]( intidx ); }; template <unsigned Size> class String {
char data[Size];
12.What is a template specialization in C++?[Understand]
Template Specialization (C++)
A template has multiple types and only some of them need to be specialized. The result is a
template parameterized on the remaining types. A template has only one type, but a
specialization is needed for pointer, reference, pointer to member, or function pointer types.
13.How to declare a template? [Apply]
Class Template Declaration
A class template starts with the keyword template followed by template parameter(s) inside
<> which is followed by the class declaration. template <class T> class className{ private:
T var; ... .. ... public: T functionName(T arg); ... .. ... };
14.What is typename in C++ template? [Understand]
In template definitions, typename provides a hint to the compiler that an unknown identifier
is a type. In template parameter lists, it's used to specify a type parameter.
15. what is the Stream Class Hierarchy in C++.[Understand]
istream is a general purpose input stream. cin is an example of an istream.
ostream is a general purpose output stream. cout and cerr are both examples of ostreams.
ifstream is an input file stream. ...
ofstream is an output file stream.
16.What is a file stream in C++? [Understand]
A stream is an abstraction that represents a device on which operations of input and output
are performed. A stream can be represented as a source or destination of characters of
indefinite length depending on its usage. In C++ we have a set of file handling methods.
These include ifstream, ofstream, and fstream
17.What is stream in Oops? [Understand]
A stream is a general name given to a flow of data. Each stream has a source and a
destination. For output stream, the source is always memory variable and the destination is
the file in which the data is to be written. For input stream, it is the reverse.
16.What are the advantages of streams in C++?[Understand]
C++ stream objects provide two advantages over their C counterparts: The C++ compiler
determines the type of variable being read or written and the stream I/O operators
automatically convert an input value to the appropriate type and automatically convert an
output value to an appropriate printed string.
18.What is a stream and a file? [Understand]
A file stream is a sequence of bytes used to hold file data. Usually a file has only one file
stream, namely the file's default data stream. However, on file systems that support multiple
data streams, each file can have multiple file streams. One of these streams is the default data
stream, which is unnamed.
19.What is stream class in C++ with an example? [Understand]
A C++ stream is a flow of data into or out of a program, such as the data written to cout or
read from cin. For this class we are currently interested in four different classes: istream is a
general purpose input stream. cin is an example of an istream.
20. what are the library functions in C++.[Understand]
We learned the basics of C++ libraries: There are 2 types of libraries: static library and
dynamic(shared) library. Static libraries are copied into the executable at compile time.
Dynamic libraries are not copied, but loaded and linked at runtime.
22.What is formatted Input and formatted output in C++?[Understand]
Formatting Output in C++
cout<< "M = " << M << " and " G = " << G; In each case, the value of the string or variable
is printed using a default format.
In this method the variables are read from a fixed starting point until a space is encountered.
As every variable has a fixed starting point, the number of columns between any pair of
variables becomes the width of the first variable.
PART-B
1.Explain in detail about the Template function in C++ with its examples.[Understand]
2.Explain in detail about the Class Template in C++ with its examples.[Understand]
3.Difference between the Function Template and Class Template in C++ with its
examples.[Understand]
PART-C