0% found this document useful (0 votes)
3 views

C++ Master Question

The document is a model question paper for C++ programming, covering various fundamental concepts such as Object-Oriented Programming, classes, constructors, inheritance, method overloading, and dynamic memory allocation. It includes questions that require explanations and code demonstrations, as well as short notes on specific topics like virtual functions, access specifiers, and abstract classes. The document serves as a comprehensive guide for understanding and practicing C++ programming principles.

Uploaded by

jevelj0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C++ Master Question

The document is a model question paper for C++ programming, covering various fundamental concepts such as Object-Oriented Programming, classes, constructors, inheritance, method overloading, and dynamic memory allocation. It includes questions that require explanations and code demonstrations, as well as short notes on specific topics like virtual functions, access specifiers, and abstract classes. The document serves as a comprehensive guide for understanding and practicing C++ programming principles.

Uploaded by

jevelj0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

C++

GUESS QUESTIONS

BY HARSHIT

Harshit: 980 190 7094


Model Question Paper - 2025

1. What is C++? Explain evolution and features of C++.


2. What is Object Oriented Programming? Explain its 4 pillars.
3. Explain Class & Object. Write a program to demonstrate.
4. What is Constructor? Explain different types of it.
5. What is Inheritance? Explain different types of it.
6. What is method overloading and how it is performed?
7. Explain Friend Function. Write a program to demonstrate.
8. What is dynamic memory allocation?
9. Explain Operator Overloading. Write a program to demonstrate.
10. Write short notes:
a. Virtual Function
b. Access Specifier
c. Static members
d. 'new' and 'delete' keyword.
e. Abstract class
f. Command Line arguments.
g. Inline Function

Harshit: 980 190 7094


1. What is C++? Explain evolution and features of C++.

C++ is a general-purpose programming language developed by Bjarne Stroustrup in


1979 at Bell Labs. It is an extension of the C language, adding support for Object-
Oriented Programming (OOP) while retaining the features of procedural programming.
Features of C++:
 Object-Oriented Programming: Supports OOP concepts like classes, objects,
inheritance, and polymorphism.
 Platform Independence: Code can run on different platforms with minimal
changes.
 Rich Library Support: Provides Standard Template Library (STL) for data structures
and algorithms.
 Low-Level Manipulation: Allows direct hardware-level operations like in C.
 Dynamic Memory Management: Uses keywords like new and delete.
 Extensibility: Users can define their own data types (e.g., classes).
 Speed: C++ is faster than many other high-level languages due to its compiled
nature.

Evolution of C++:
 1979: Bjarne Stroustrup started developing "C with Classes."
 1983: Officially renamed as "C++."
 1990: ANSI C++ standardization started.
 1998: C++98 was introduced with core features.
 2011, 2014, 2017, 2020: New standards like C++11, C++14, C++17, and C++20 added
modern features like lambda expressions, smart pointers, and modules.

Difference between C and C++

Aspect C C++

Programming Style Procedural Procedural + Object-Oriented

Focus Functions Objects and Classes

Encapsulation Not Supported Supported

Inheritance Not Supported Supported

Memory Allocation malloc, free new, delete

Harshit: 980 190 7094


Basic Structure of a C++ Program

Preprocessor Directives:

 #include statements to include libraries.


 Example: #include <iostream>

Main Function:

 The starting point of the program.


 Syntax: int main() { }

Variable Declarations:

 Declare variables inside the main() or other functions.


 Example: int a;

Statements and Expressions:

 Perform operations, calculations, and assign values.


 Example: a = 5 + 3;

Input and Output:

 Use cin for input and cout for output.


 Example: cout << "Hello, World!";

Return Statement:

 Ends the main() function and returns a value.


 Example: return 0;

Example program:

Harshit: 980 190 7094


2. What is Object Oriented Programming? Explain its 4 pillars.
Object-Oriented Programming (OOP) is a programming paradigm that organizes
software design around data, or objects, rather than functions and logic. Objects are
instances of classes, which group data (attributes) and functions (methods) together.
4 Pillars of OOP:
1. Encapsulation: Binding data and functions that operate on the data into a single unit
(class). Prevents unauthorized access using access specifiers (private, protected,
public). For Example:

2. Abstraction: Hides implementation details and shows only the essential features of
an object. Achieved using abstract classes and interfaces. For Example:

Harshit: 980 190 7094


3. Inheritance: Enables a new class (child) to inherit attributes and methods from an
existing class (parent). Promotes reusability and reduces redundancy. For Example:

4. Polymorphism: Allows a single function or method to behave differently based on


the context. Achieved through function overloading, operator overloading, and method
overriding. For Example:

1. Compile-Time Polymorphism (Static Binding):


The function to be executed is determined during compile time.
Achieved using:
 Function Overloading: Multiple functions with the same name but different
parameters.
 Operator Overloading: Overloading operators like +, *, etc., to work with user-
defined types.

2. Run-Time Polymorphism (Dynamic Binding):


The function to be executed is determined at runtime.
Achieved using:
 Virtual Functions: Enables method overriding where the child class provides a
specific implementation of a base class method.
 Requires a base class pointer or reference to call the overridden method.

Harshit: 980 190 7094


3. Explain Class & Object. Write a program to demonstrate.
A class is a blueprint or template that defines the structure and behavior (attributes
and methods) of objects. It acts as a user-defined data type.
 Blueprint: A class is a blueprint for creating objects. It defines attributes (data) and
methods (functions).
 Encapsulation: Combines data and functions into a single unit.
 Access Specifiers:
 private: Members accessible only within the class.
 public: Members accessible from outside the class.
 protected: Members accessible in derived classes.
 Structure: Defined using the class keyword.
 No Memory Allocation: A class does not consume memory until an object is created.

An object is an instance of a class. It holds the data defined in the class and can
perform operations using the methods of the class.

 Instance: An object is a real-world entity created from a class.


 Attributes and Behavior: Objects hold data (attributes) and use methods
(behavior) defined in the class.
 Memory Allocation: Memory is allocated to an object when it is created.
 Object Access: Access class members using the dot operator
(objectName.member).

Harshit: 980 190 7094


Example program of Class & Object.

Output:

Harshit: 980 190 7094


4. What is Constructor? Explain different types of it.
A constructor is a special member function of a class that initializes objects when they
are created. It has the same name as the class and no return type.
Key Points:
 Automatically called when an object is created.
 Used to initialize object attributes.
 Can be overloaded to create different types of constructors.
 Does not have a return type (not even void).
 Declared in the public section of a class.

1. Default Constructor: A constructor with no parameters. Used to provide default


values to object attributes..

2. Parameterized Constructor: A constructor that takes parameters to initialize the


object with specific values.

Harshit: 980 190 7094


3. Copy Constructor: A constructor that creates a new object as a copy of an existing
object. Takes a reference to an object of the same class as a parameter.

Syntax:

Example Program:

Output:

Harshit: 980 190 7094


5. What is Inheritance? Explain different types of it.
Inheritance is a feature in object-oriented programming that allows a class (child
class or derived class) to inherit attributes and methods from another class (parent
class or base class). It promotes code reuse and establishes a relationship between
classes.
Key Points about Inheritance:
 Purpose:
o Reusability of code.
o Establishes a parent-child relationship between classes.
 Access Specifiers: Determines how members of the base class are accessible in the
derived class (private, protected, public).
 Base and Derived Class:
o Base Class: The class whose members are inherited.
o Derived Class: The class that inherits members from the base class.
Syntax:

Example Program Inheritance:

Harshit: 980 190 7094


1. Single Inheritance: A single derived class inherits from one base class.

2. Multiple Inheritance: A derived class inherits from more than one base class.

Harshit: 980 190 7094


3. Multilevel Inheritance: A class derives from another derived class, forming a chain.

4. Hierarchical Inheritance: Multiple derived classes inherit from a single base class.

Harshit: 980 190 7094


5. Hybrid Inheritance: Combines two or more types of inheritance, typically multiple
and multilevel inheritance. May lead to ambiguity, resolved using the virtual keyword.

Harshit: 980 190 7094


Types of Inheritance (Summary):

Harshit: 980 190 7094


6. What is method overloading and how it is performed?
Method overloading is a feature in C++ where two or more functions in the same scope
have the same name but differ in the number or type of their parameters. It allows a
function to behave differently based on the arguments passed to it.
Points about Method Overloading:
 It allows multiple functions with the same name but different parameters.
 The return type is not considered for overloading.
 It is determined at compile-time (early binding).
 It improves code readability and reuse.

Example Program: Method Overloading

Output:

Harshit: 980 190 7094


7. Explain Friend Function. Write a program to demonstrate.
A friend function is a function that is not a member of a class but has access to its
private and protected members. It is declared using the friend keyword inside the
class. Friend functions can be regular functions, member functions of another class, or
even global functions.

Key Points about Friend Function:


 A friend function is defined outside the class but has access to private and
protected members of the class.
 It can access the private and protected members of any class to which it is
declared as a friend.
 It is not a member of the class, so it cannot be called with the dot (.) operator.
 Friend functions are often used when two or more classes need to work closely
together and require access to each other's private data.

Harshit: 980 190 7094


8. Explain Operator Overloading. Write a program to demonstrate.
Operator overloading is a feature in C++ that allows you to redefine the functionality of
operators (e.g., +, -, *, etc.) for user-defined classes. This enables objects of a class to
be used with operators in a way that is intuitive and meaningful for the class.
Key Points about Operator Overloading:
 Syntax: Operator overloading is done by defining a special function with the
keyword operator followed by the operator symbol.
 Access: Operator overloading can be done as a member function or a non-
member (friend) function.
 Only Existing Operators: Only existing operators can be overloaded, but you
cannot create new operators.
 Return Type: The overloaded operator usually returns a value based on the
operator’s function.
Example Program:

Harshit: 980 190 7094


9. What is dynamic memory allocation?
Dynamic memory allocation in C++ refers to the process of allocating memory at
runtime using pointers. It allows for the creation of variables or arrays whose size is
determined during program execution, rather than at compile time.

Key Points About Dynamic Memory Allocation:


Memory Allocation at Runtime: Memory is allocated during program execution
(runtime) using operators like new and new[].
Deallocation of Memory: Memory allocated dynamically must be manually deallocated
using delete and delete[] to avoid memory leaks.
Usage: Useful when the size of the data structure or array cannot be known in advance
and may change during program execution.
Operators:
 new: Allocates memory for a single object or array.
 delete: Deallocates memory allocated with new.
 new[]: Allocates memory for an array of objects.
 delete[]: Deallocates memory allocated with new[].

Output:

Harshit: 980 190 7094


10. Write short notes:
a. Virtual Function
b. Access Specifier
c. Static members
d. 'new' and 'delete' keyword.
e. Abstract class
f. Command Line arguments.
g. Inline Function

a. Virtual Function: A virtual function in C++ is a member function of a class that is


declared using the keyword virtual. It allows derived classes to override this function to
provide their own implementation, ensuring dynamic polymorphism.
Key Points:
 Virtual functions enable function overriding in derived classes.
 The virtual keyword must be used in the base class.
 The correct function is called at runtime based on the object type, not the pointer
type (dynamic polymorphism).

Output:

Harshit: 980 190 7094


b. Access Specifiers: Access specifiers are keywords in C++ that define the
accessibility of members (variables and functions) of a class. The three access
specifiers are public, private, and protected.
Key Points:
 Public: Members are accessible from anywhere.
 Private: Members are accessible only within the class.
 Protected: Members are accessible within the class and by derived classes.

Output:

Harshit: 980 190 7094


c. Static Members: Static members in C++ are shared among all instances of a class.
A static member is declared using the keyword static, and it can be a variable or a
function.
Key Points:
 Static members are shared by all objects of the class.
 Static variables are initialized once and retain their value across function calls.
 Static functions can only access static members.

Output:

Harshit: 980 190 7094


d. 'new' and 'delete' Keyword: The new and delete keywords are used for dynamic
memory allocation and deallocation in C++.
Key Points:
 new allocates memory on the heap and returns a pointer.
 delete deallocates memory allocated by new.
 new[] and delete[] are used for arrays.

Output:

Harshit: 980 190 7094


e. Abstract Class: An abstract class is a class that cannot be instantiated directly. It is
defined by having at least one pure virtual function, i.e., a function that is declared but
not defined in the class.
Key Points:
 Abstract classes cannot create objects.
 Derived classes must implement all pure virtual functions to become concrete.

Output:

Harshit: 980 190 7094


f. Command Line Arguments: Command line arguments are parameters passed to a
program when it is executed from the command line. These arguments are accessed
using argc (argument count) and argv (argument vector).
Key Points:
 argc holds the number of command line arguments.
 argv is an array of C-style strings representing the arguments.

Example Command:

Output:

Harshit: 980 190 7094


g. Inline Function: An inline function is a function whose code is inserted at the point of
call, rather than being called through a traditional function call mechanism.
Key Points:
 Inline functions are usually small and simple.
 The inline keyword suggests to the compiler to insert the function’s code at the
point of the call.

Output:

Thanks for Reading


Team Harshit

Harshit: 980 190 7094

You might also like