0% found this document useful (0 votes)
3 views29 pages

Part

The document covers fundamental concepts of object-oriented programming (OOP) in C++, including characteristics of objects, classes, inheritance, polymorphism, encapsulation, and data hiding. It explains various programming constructs such as friend functions, operator overloading, constructors, and file handling modes. Additionally, it contrasts OOP with functional programming and discusses the use of scope resolution and different types of operators in C++.

Uploaded by

himsharma3007
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)
3 views29 pages

Part

The document covers fundamental concepts of object-oriented programming (OOP) in C++, including characteristics of objects, classes, inheritance, polymorphism, encapsulation, and data hiding. It explains various programming constructs such as friend functions, operator overloading, constructors, and file handling modes. Additionally, it contrasts OOP with functional programming and discusses the use of scope resolution and different types of operators in C++.

Uploaded by

himsharma3007
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

PART –I

Note: Answer the following questions. All questions are compulsory. Each question carries
two (02) marks.
1. Identity, State, and Behavior of an Object
An object has three characteristics. Identity is the unique name or reference of the object.
State represents the data values stored in the object at a particular time. Behavior defines
the actions or functions performed by the object.

2. Relation between Class and Object


A class is a user-defined blueprint that defines data members and member functions. An
object is a real-world entity and an instance of a class. Multiple objects can be created from
a single class.

3. Derived Data Types


Derived data types are formed using basic data types. They help in storing complex data
efficiently. Examples include arrays, pointers, structures, unions, and classes.

4. Implicit and Explicit Type Conversion


Implicit type conversion is automatically performed by the compiler when converting
smaller data types to larger ones. Explicit type conversion is done manually using type
casting.
Example: int a=10; float b=(float)a;

5. Use of Friend Function


A friend function is not a member of a class but can access its private and protected
members. It is declared using the keyword friend. Friend functions are useful when two or
more classes need to share data.

6. This Pointer in C++


The this pointer is an implicit pointer available inside all non-static member functions. It
stores the address of the current calling object. It is mainly used to differentiate between
data members and parameters with the same name.
7. Base Class and Derived Class
A base class is a class whose properties are inherited by another class. A derived class is a
class that inherits the properties and functions of a base class. Inheritance improves code
reusability.

8. Runtime Polymorphism in C++


Runtime polymorphism is achieved using function overriding. It is implemented using
virtual functions and base class pointers. The function call is resolved at runtime based on
object type.

9. Generic Programming
Generic programming allows writing code that works for different data types. It is
implemented using templates in C++. This increases code reusability and reduces
redundancy.

10. Stream
A stream is a flow of data between the program and input/output devices. C++ uses streams
for input and output operations. Examples include cin for input and cout for output.

11. Core Concepts of OOP, Class and Object


Core concepts of OOP include Encapsulation, Abstraction, Inheritance, and
Polymorphism. A class is a user-defined data type containing data and functions. An
object is an instance of a class.

12. Encapsulation and Data Hiding


Encapsulation is the process of binding data and functions together into a single unit. Data
hiding restricts direct access to data using access specifiers. It helps in improving security
and data integrity.

13. Abstraction, Inheritance, and Polymorphism


Abstraction shows only essential details and hides implementation. Inheritance allows one
class to acquire properties of another class. Polymorphism allows a function to perform
different actions based on the object.

14. Inheritance in C++ and Types


Inheritance is a mechanism in which one class acquires the properties of another class. It
promotes code reuse and extensibility. Types of inheritance are Single, Multiple,
Multilevel, Hierarchical, and Hybrid.

15. Function Overloading


Function overloading allows multiple functions with the same name but different
parameters. It is an example of compile-time polymorphism. The correct function is
selected based on arguments.

16. Operator Overloading


Operator overloading allows redefining the behavior of operators for user-defined data
types. It improves code readability and usability. Example operators include +, -, *, and ==.

17. Constructor and Destructor


A constructor is a special member function used to initialize objects. It is automatically
called when an object is created. A destructor is used to destroy objects and release memory.

18. Types of Constructors


A default constructor has no parameters. A copy constructor initializes an object using
another object of the same class. A parameterized constructor accepts arguments to
initialize data members.

19. Virtual Function and Pure Virtual Function


A virtual function is used to achieve runtime polymorphism. A pure virtual function has no
definition and is declared using =0. Classes containing pure virtual functions become
abstract classes.
20. Runtime and Compile-time Polymorphism
Compile-time polymorphism is achieved through function and operator overloading.
Runtime polymorphism is achieved using virtual functions and inheritance. Runtime
binding occurs during program execution.

21. Namespace
A namespace is used to group related identifiers together. It avoids naming conflicts in large
programs. The using keyword is used to access namespace members.

22. Friend Function


A friend function can access private and protected members of a class. It is declared inside
the class using the friend keyword. It is useful when external functions need access to class
data.

23. Inline Function vs Normal Function


An inline function expands its code at the place of function call. It reduces function call
overhead and improves execution speed. A normal function follows the usual calling
mechanism.

24. This Pointer


The this pointer refers to the calling object of a class. It is used when data members and
function parameters have the same name. It also helps in returning the calling object.

25. Static Data Member


A static data member is shared by all objects of a class. Only one copy exists regardless of
the number of objects. It is accessed using the class name.

26. Access Specifier


Access specifiers define the accessibility of class members. They control data hiding and
security. The three types are public, private, and protected.
27. Abstract Class
An abstract class is a class that contains at least one pure virtual function. It cannot be
instantiated directly. It is mainly used to achieve abstraction in C++.

PART –II
Note: Answer the following questions. All questions are compulsory. Each question carries
04 marks.

1. Briefly explain the various characteristics of object-oriented language.


Object-oriented language is based on real-world entities. Encapsulation binds data and
functions together in a class. Abstraction hides internal details and shows only essential
features. Inheritance allows one class to acquire properties of another, increasing code
reusability. Polymorphism enables the same function or operator to behave differently in
different situations.

2. What are the various types of statements in C++? Explain Jumping statements with
example.
C++ statements are classified into simple statements, compound statements, control
statements, and jumping statements.
Jumping statements change the normal flow of execution. They include break, continue,
goto, and return.
The break statement terminates a loop or switch statement immediately.
Example:
for(int i=1;i<=5;i++)
{
if(i==3)
break;
cout<<i<<" ";
}
3. What is function? Explain different ways for defining member function in C++ with
appropriate code.
A function is a block of code that performs a specific task and can be reused.
Member functions of a class can be defined inside the class or outside the class using
scope resolution operator (::).
Defining outside the class improves readability and program structure.
Example:
class Test {
public:
void show();
};

void Test::show() {
cout<<"Hello C++";
}

4. What is operator overloading? Write a C++ program illustrating Overloading


binary operators?
Operator overloading allows operators to work with user-defined data types.
It improves program readability and usability.
Binary operator overloading involves two operands.
Program:
#include<iostream>
using namespace std;

class Number {
int x;
public:
Number(int a){ x=a; }
Number operator +(Number obj) {
return Number(x + obj.x);
}
void display() {
cout<<x;
}
};

int main() {
Number n1(10), n2(20);
Number n3 = n1 + n2;
[Link]();
}

5. Explain different file opening modes in C++ with example.


File opening modes specify how a file should be accessed.
ios::in opens a file for reading, ios::out opens for writing, ios::app appends data at the end,
and ios::binary opens a file in binary mode.
These modes can be combined while opening a file.
Example:
ofstream file;
[Link]("[Link]", ios::out);
file<<"C++ File Handling";
[Link]();

6. What is a friend function ? How to use a friend function in C++? Explain benefits of
friend function and give example.
A friend function is not a member of a class but can access its private and protected data.
It is declared inside the class using the keyword friend.
Friend functions are useful when external functions or other classes need access to private
data.
Example:
class Sample {
int a = 5;
public:
friend void show(Sample);
};

void show(Sample s) {
cout<<s.a;
}
Benefits:
 Access private members
 Improves flexibility
 Useful in operator overloading

PART- III
(a) What is encapsulation? How is it different from information hiding? Explain with
the help of an example.
Encapsulation is an important concept of object-oriented programming in which data
members and member functions are combined into a single unit called a class. It helps in
organizing the program in a structured manner and improves modularity. Encapsulation
controls how data is accessed and modified within a program. Information hiding is closely
related to encapsulation and refers to restricting direct access to internal data of a class.
Only selected data is exposed through public functions. This protects data from misuse and
increases program security.
1. Encapsulation binds data and functions together.
2. Information hiding restricts direct access to data members.
3. Encapsulation improves program structure and modularity.
4. Information hiding improves data security.
5. Encapsulation is implemented using classes.
6. Information hiding is implemented using access specifiers.
class Student {
private:
int marks;
public:
void setMarks(int m) { marks = m; }
int getMarks() { return marks; }
};

(b) Briefly discuss applications and advantages of object-oriented paradigm?


The object-oriented paradigm is a programming approach that models software using real-
world objects. It is widely used for developing large and complex software systems. OOP
makes programs more manageable by dividing them into objects. It supports better
organization of code and easier modification. Due to its flexible nature, OOP is preferred
for modern software development. It is suitable for applications that require scalability and
maintainability.
1. Used in banking and financial applications.
2. Applied in web and mobile application development.
3. Suitable for game and GUI-based applications.
4. Supports code reusability through inheritance.
5. Provides better data security using encapsulation.
6. Makes programs easy to maintain and extend.

OR

(a) Define Abstraction and Message passing in object oriented programming. Explain
how message passing is used in C++ programming with example.
Abstraction refers to hiding internal implementation details and showing only essential
features to the user. It helps reduce complexity and allows the programmer to focus on
functionality. Message passing is the process by which objects communicate with each
other. In this mechanism, one object sends a message to another object by calling its
member function. This interaction enables cooperation between objects. Message passing
improves modularity and flexibility in object-oriented programs.
1. Abstraction hides unnecessary implementation details.
2. It shows only relevant information to the user.
3. Message passing allows communication between objects.
4. It is implemented using object and function calls.
5. Improves interaction among objects.
6. Supports modular and flexible program design.
class Demo {
public:
void show() {
cout << "Message received";
}
};

int main() {
Demo d;
[Link]();
}

(b) Compare functional programming and OOP approach with relevant examples.
Functional programming and object-oriented programming are two different approaches to
software development. Functional programming focuses mainly on functions and logic.
Data and functions are treated separately in this approach. Object-oriented programming
focuses on objects that represent real-world entities. In OOP, data and functions are
combined into a single unit. This approach is more suitable for large and complex
programs.
1. Functional programming is function-oriented.
2. OOP is object-oriented.
3. Functional programming does not support data hiding.
4. OOP supports encapsulation and data hiding.
5. Functional programming example is C language.
6. OOP example is C++ language.

(c) Discuss the main difference between C and C++ ?


C and C++ are popular programming languages but differ in their approach and features. C
follows a procedural programming approach. It focuses mainly on functions and step-by-
step execution. C++ supports object-oriented programming concepts. It allows the use of
classes and objects. C++ is more suitable for large-scale software development compared to
C.
1. C is a procedural language.
2. C++ is an object-oriented language.
3. C does not support classes and objects.
4. C++ supports classes and objects.
5. C provides less data security.
6. C++ provides better data security.

2. (a) Define and explain the following type of instructions with their syntax:
These instructions are basic building blocks of a C++ program. They help in program
compilation, documentation, input, and output operations. Each instruction has a specific
role in program execution. Understanding their syntax is essential for writing correct
programs. These instructions are commonly used in almost every C++ program. Proper use
improves readability and functionality.
(i) Preprocessor directive
Preprocessor directives are instructions given to the compiler before actual compilation
starts. They begin with the # symbol and are used to include header files or define
constants. They do not end with a semicolon. These directives help in managing program
structure. They make programs modular and reusable.
1. Processed before compilation
2. Start with # symbol
3. No semicolon used
4. Used for file inclusion
5. Used for macro definition
Syntax:
#include <iostream>
#define PI 3.14
(ii) Comments
Comments are non-executable statements used to explain the code. They improve
readability and understanding of the program. Comments are ignored by the compiler. They
are useful for documentation purposes. Comments help other programmers understand the
logic.
1. Not executed by compiler
2. Improve code readability
3. Used for explanation
4. Helpful in debugging
5. Support documentation
Syntax:
// single line comment
/* multi line comment */
(iii) Output using cout
cout is used to display output on the screen. It is part of the iostream library. The insertion
operator << is used with cout. It sends data from the program to the output screen. Multiple
values can be displayed together.
1. Used for output
2. Belongs to iostream
3. Uses << operator
4. Displays text and values
5. Can print multiple items
Syntax:
cout << "Hello World";
(iv) Input using cin
cin is used to take input from the user. It also belongs to the iostream library. The extraction
operator >> is used with cin. It reads data from the keyboard. It allows interaction with the
user.
1. Used for input
2. Takes data from keyboard
3. Uses >> operator
4. Reads user values
5. Supports multiple inputs
Syntax:
cin >> x;

(b) What is the use of scope resolution operator? Write down the names of any 04
operators present in C++ that cannot be overloaded.
The scope resolution operator :: is used to define or access class members outside the class.
It is also used to access global variables when local variables have the same name. This
operator helps in resolving naming conflicts. It improves clarity in large programs. It is
widely used in object-oriented programming.
1. Used to access class members outside class
2. Used to access global variables
3. Resolves name conflicts
4. Improves code clarity
5. Commonly used in C++
Operators that cannot be overloaded:
1. ::
2. sizeof
3. ?:
4. .

(c) Explain different types of Operators in c++ with example?


Operators are symbols used to perform operations on variables and values. C++ provides
different types of operators for various operations. These operators make programming
easier and efficient. Each operator type performs a specific task. They are essential for
expression evaluation.
1. Arithmetic operators (+, -, *, /)
2. Relational operators (>, <, ==)
3. Logical operators (&&, ||, !)
4. Assignment operators (=, +=)
5. Increment and decrement operators (++, --)
Example:
int a = 5, b = 3;
cout << a + b;
OR
(a) Write a program that takes an alphabet as input and using if else ladder statement
find out whether the alphabet is a vowel or not.
This program checks whether a given alphabet is a vowel or a consonant. It uses an if–else
ladder to compare the input character. Both uppercase and lowercase vowels are checked.
The program takes input from the user. Based on the condition, it displays the result.
#include <iostream>
using namespace std;

int main() {
char ch;
cin >> ch;

if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' ||


ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
cout << "Vowel";
else
cout << "Consonant";

return 0;
}
1. Takes character input
2. Uses if–else ladder
3. Checks lowercase vowels
4. Checks uppercase vowels
5. Displays result

(b) What are Datatypes? List different types of user defined datatypes available in C++.

Datatypes specify the kind of data that a variable can store in a program. They help the
compiler decide how much memory should be allocated for a variable. Datatypes also
define the range of values and the operations that can be performed on the data. Proper use
of datatypes improves program efficiency and readability. In C++, datatypes are broadly
classified into basic, derived, and user-defined datatypes. User-defined datatypes provide
flexibility to create new data types according to program requirements.
 Structure: Allows grouping of variables of different data types under a single name.
 Union: Similar to structure, but all members share the same memory location.
 Class: Combines data members and member functions into a single unit, supporting
object-oriented programming.
 Enumeration (enum): Used to define a set of named integer constants, improving code
clarity.
 Typedef: Used to create an alias or alternative name for an existing data type.

3. Write a C++ program to define class "Circle" and implement the following:
(i) a constructor
(ii) a member function to find area of a circle
(iii) a member function to find circumference of a circle
A class in C++ is a user-defined data type that groups data members and member functions
into a single unit. The Circle class represents a real-world circle and stores its radius as
data. A constructor is used to initialize the radius when an object is created. Member
functions are used to perform operations such as calculating area and circumference. This
approach follows the object-oriented concept of data encapsulation. Using a class improves
code reusability and clarity.
C++ Program:
#include <iostream>
using namespace std;

class Circle
{
float radius;

public:
Circle(float r)
{
radius = r;
}

float area()
{
return 3.14 * radius * radius;
}

float circumference()
{
return 2 * 3.14 * radius;
}
};

int main()
{
Circle c(5);

cout << "Area of Circle = " << [Link]() << endl;


cout << "Circumference of Circle = " << [Link]() << endl;

return 0;
}

OR
(a) Write a C++ program to demonstrate how objects are passed as an argument to a
function.
In C++, objects can be passed to functions in the same way as variables. Passing objects
allows functions to access and manipulate data of a class. This is useful when operations
depend on multiple object values. Objects are usually passed by value or by reference.
Passing objects promotes modular and reusable code.
C++ Program:
#include <iostream>
using namespace std;

class Sample
{
int x;

public:
void set(int a)
{
x = a;
}

void add(Sample s)
{
cout << "Sum = " << x + s.x << endl;
}
};

int main()
{
Sample s1, s2;
[Link](10);
[Link](20);

[Link](s2);

return 0;
}

(b) What is copy constructor? Illustrate the use of copy constructor with help of a
program in C++.
A copy constructor is a special constructor used to initialize a new object using an existing
object of the same class. It is automatically called when an object is passed by value,
returned from a function, or explicitly copied. The copy constructor helps in copying data
safely between objects. It ensures proper duplication of resources. If not defined, C++
provides a default copy constructor.
C++ Program:
#include <iostream>
using namespace std;

class Demo
{
int x;

public:
Demo(int a)
{
x = a;
}

Demo(Demo &d)
{
x = d.x;
}

void display()
{
cout << "Value of x = " << x << endl;
}
};

int main()
{
Demo d1(50);
Demo d2 = d1;

[Link]();
[Link]();

return 0;
}

4.
(a) Explain different types of Inheritance with proper diagram and example.
Inheritance is a mechanism in C++ through which one class acquires the properties and behavior
of another class. The class that provides data members and functions is called the base class,
while the class that inherits them is called the derived class. Inheritance supports code reusability
and hierarchical classification. It also improves program maintainability. C++ supports multiple
forms of inheritance. Each type is used according to program requirements.
Types of Inheritance:
 Single Inheritance – One derived class inherits from one base class
 Multiple Inheritance – One derived class inherits from more than one base class
 Multilevel Inheritance – A class is derived from another derived class
 Hierarchical Inheritance – Multiple derived classes inherit from one base class
 Hybrid Inheritance – Combination of two or more types of inheritance
Example (Single Inheritance):
#include <iostream>
using namespace std;

class A
{
public:
void show()
{
cout << "Base Class" << endl;
}
};

class B : public A
{
};

int main()
{
B obj;
[Link]();
return 0;
}

(b) What is function overloading? Compare default arguments with function overloading.
Function overloading is a feature of C++ that allows multiple functions to have the same name
but different parameter lists. It increases program readability and flexibility. The compiler
decides which function to call based on the number or type of arguments. Default arguments
provide predefined values to function parameters. Both techniques reduce the need for multiple
function names but work differently.
Function Overloading:
 Same function name with different parameters
 Decision made at compile time
 Improves readability and flexibility
Default Arguments:
 Same function name with optional parameters
 Values are assigned automatically
 Less flexible than overloading
Comparison:
 Overloading uses multiple functions, default arguments use one function
 Overloading supports different data types, default arguments do not
 Overloading is more powerful and flexible

OR
(a) What is the role of a constructor and destructor. Explain implicit and explicit default
constructor with example.
A constructor is a special member function used to initialize objects of a class. It is automatically
called when an object is created. A destructor is used to release memory and other resources
when an object is destroyed. Constructors help in proper initialization, while destructors ensure
clean termination. C++ supports both implicit and explicit default constructors.
Key Points:
 Constructor name is same as class name
 Destructor name starts with ~
 Implicit constructor is provided by compiler
 Explicit constructor is defined by programmer
Example:
#include <iostream>
using namespace std;

class Demo
{
public:
Demo()
{
cout << "Constructor called" << endl;
}
~Demo()
{
cout << "Destructor called" << endl;
}
};

int main()
{
Demo obj;
return 0;
}

(b) Explain briefly the importance of pure virtual function. Write a C++ program with
abstract class having pure virtual function.
A pure virtual function is a function declared using =0 and has no definition in the base class. A
class containing a pure virtual function is called an abstract class. It is used to achieve runtime
polymorphism. Pure virtual functions enforce derived classes to provide their own
implementation. They help in designing interfaces.
Key Points:
 Declared using =0
 Abstract class cannot create objects
 Ensures implementation in derived class
 Supports runtime polymorphism
Example:
#include <iostream>
using namespace std;

class Shape
{
public:
virtual void draw() = 0;
};

class Circle : public Shape


{
public:
void draw()
{
cout << "Drawing Circle" << endl;
}
};

int main()
{
Shape *s;
Circle c;
s = &c;
s->draw();
return 0;
}

(c) What is Function template and class Template? Explain the role of template
programming with example.
Templates allow writing generic programs in C++. A function template works with different data
types using a single definition. A class template allows creating classes that handle multiple data
types. Template programming reduces code duplication. It increases reusability and efficiency.
Templates are resolved at compile time.
Key Points:
 Supports generic programming
 Same code works for different data types
 Improves reusability
 Reduces program size
Example:
#include <iostream>
using namespace std;

template <class T>


T add(T a, T b)
{
return a + b;
}

int main()
{
cout << add(10, 20) << endl;
cout << add(5.5, 2.5) << endl;
return 0;
}

5.
a) How to open and close a file in C++? Write a C++ Program to Display Contents of a text
file using File Handling.
File handling in C++ is used to store data permanently in files. Files are opened using file stream
classes such as ifstream, ofstream, and fstream. A file is opened using the open() function or
directly through the constructor. After performing operations, the file must be closed using the
close() function. Proper closing of files prevents data loss. File handling improves data
management and storage efficiency.
 ifstream is used for reading data from a file
 ofstream is used for writing data to a file
 fstream is used for both reading and writing
Program to display contents of a text file:
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ifstream file("[Link]");
char ch;

while ([Link](ch))
{
cout << ch;
}

[Link]();
return 0;
}

b) Explain File Handling in C++ in detail. Discuss file streams, file modes, and file
operations with suitable examples.
File handling allows reading and writing data to files stored on secondary memory. C++ provides
file stream classes to perform file operations efficiently. File streams manage the flow of data
between the program and files. Different file modes specify how a file should be opened. File
operations include opening, reading, writing, and closing files.
File Streams:
 ifstream – read data from file
 ofstream – write data to file
 fstream – read and write data
File Modes:
 ios::in – open file for reading
 ios::out – open file for writing
 ios::app – append data at end
 ios::binary – open file in binary mode
Example:
ofstream file("[Link]", ios::out);
file << "Hello File";
[Link]();

c) What is an Exception in C++? Explain how exception handling is done in C++ with the
help of a program.
An exception is an abnormal condition that occurs during program execution. It disrupts the
normal flow of a program. C++ provides exception handling to handle runtime errors gracefully.
Exceptions are handled using try, throw, and catch blocks. This prevents program termination
and improves reliability.
 try block contains risky code
 throw generates an exception
 catch handles the exception
Program for exception handling:
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 0;

try
{
if (b == 0)
throw b;
cout << a / b;
}
catch (int x)
{
cout << "Division by zero error";
}

return 0;
}

d) Write a program to copy contents of one file to another.


Copying file contents involves reading data from a source file and writing it into a destination
file. This operation uses input and output file streams together. Character by character copying
ensures accurate data transfer. File handling makes file duplication simple and efficient. Proper
closing of files is necessary after copying.
Program to copy contents of one file to another:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream source("[Link]");
ofstream destination("[Link]");
char ch;

while ([Link](ch))
{
[Link](ch);
}

[Link]();
[Link]();
return 0;
}

You might also like