Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
47 views

C++ Question Bank- Module 1

Questions

Uploaded by

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

C++ Question Bank- Module 1

Questions

Uploaded by

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

Department of Computer Science and Engineering

Acharya Institute of Technology


Affiliated to VTU, Recognized by GOK and Approved by AICTE, New Delhi (Accredited by NAAC and NBA)
Acharya Dr. Sarvepalli Radhakrishnan Road, Acharya P.O., Soladevanahalli, Bangalore-560107, INDIA
www.acharya.ac.in, Email: hod-cse@acharya.ac.in

Introduction to C++ programming

MODULE-I

1. Explain the features of Object-oriented programming (Polymorphism,


Inheritance, Encapsulation).
Features of Object-Oriented Programming are:
1. Emphasis on Data rather than Procedure.
2. Programs are divided into objects.
3. Data is hidden and cannot be accessed by external functions.
4. Objects may communicate with each other through functions.
5. New data and functions can be easily added whenever necessary.
6. Follows bottom-up approach in program design.
Polymorphism:
Polymorphism in C++ is one of the key concepts of Object-Oriented Programming
(OOP) that allows objects of different types to be treated uniformly through a
common interface. Polymorphism allows the same function or operation to behave
differently based on the object or context in which it is used.
Example:
a. Using the same function name to perform different tasks with varying
parameters is called function overloading.
b. Operator overloading involves overloading the “+” operator to handle both
integer addition and string concatenation in an abstract way.

Department of Computer Science and Engineering 1


Department of Computer Science and Engineering
Acharya Institute of Technology
Affiliated to VTU, Recognized by GOK and Approved by AICTE, New Delhi (Accredited by NAAC and NBA)
Acharya Dr. Sarvepalli Radhakrishnan Road, Acharya P.O., Soladevanahalli, Bangalore-560107, INDIA
www.acharya.ac.in, Email: hod-cse@acharya.ac.in

Inheritance:
Inheritance allows a class (called a derived class) to inherit properties and behaviors
(methods) from another class (called the base class). It promotes code reuse and
establishes a parent-child relationship between classes.

Encapsulation:
Encapsulation is the bundling of data and methods that operate on that data within
a class. It restricts direct access to some of the object's components, which is
essential for protecting the object's integrity. It allows for data hiding by using
access specifiers like private, protected, and public.

Department of Computer Science and Engineering 2


Department of Computer Science and Engineering
Acharya Institute of Technology
Affiliated to VTU, Recognized by GOK and Approved by AICTE, New Delhi (Accredited by NAAC and NBA)
Acharya Dr. Sarvepalli Radhakrishnan Road, Acharya P.O., Soladevanahalli, Bangalore-560107, INDIA
www.acharya.ac.in, Email: hod-cse@acharya.ac.in

2. Bring out the difference between object oriented and structured oriented
programming.

Aspect Object-Oriented Programming (OOP) Structured Programming

Focuses on objects that encapsulate data Focuses on procedures or functions to


Basic Concept
and behavior. operate on data.

Encapsulation, Inheritance, Top-down design, Control flow,


Core Concepts
Polymorphism, Abstraction Modularity through functions

Code
Organized around objects and classes. Organized around functions or procedures.
Organization

Data and
Data and functions are combined in
Function Data is often separate from functions.
objects.
Relationship

High reusability through inheritance and Reusability is limited to functions but lacks
Reusability
polymorphism. inheritance and polymorphism.

Easier maintenance due to encapsulation Maintenance can be harder for larger


Maintainability
and modular design. programs as global data may be affected.

Easily extensible via inheritance and Less flexible in extending functionality,


Extensibility
polymorphism. relying on modifying existing code.

Data is encapsulated and accessed via Less focus on data security, data can be
Data Security
methods, ensuring better security. easily modified by any function.

Real-World Models real-world entities through Focuses more on step-by-step problem-


Modeling objects. solving.

Examples of
Java, C++, Python, C#, Ruby, Swift C, Pascal, Fortran, BASIC
Languages

A program with functions like deposit(),


A Car class with attributes like speed and
Example withdraw(), and a separate data structure
methods like accelerate().
for account details.

Department of Computer Science and Engineering 3


Department of Computer Science and Engineering
Acharya Institute of Technology
Affiliated to VTU, Recognized by GOK and Approved by AICTE, New Delhi (Accredited by NAAC and NBA)
Acharya Dr. Sarvepalli Radhakrishnan Road, Acharya P.O., Soladevanahalli, Bangalore-560107, INDIA
www.acharya.ac.in, Email: hod-cse@acharya.ac.in

3. Illustrate the concept of polymorphism.


Polymorphism in C++ is one of the key concepts of Object-Oriented Programming
(OOP) that allows objects of different types to be treated uniformly through a
common interface. Polymorphism allows the same function or operation to behave
differently based on the object or context in which it is used.
Example program showing function overloading for the following prototypes.
add(int a, int b) add(double a, double b)

Department of Computer Science and Engineering 4


Department of Computer Science and Engineering
Acharya Institute of Technology
Affiliated to VTU, Recognized by GOK and Approved by AICTE, New Delhi (Accredited by NAAC and NBA)
Acharya Dr. Sarvepalli Radhakrishnan Road, Acharya P.O., Soladevanahalli, Bangalore-560107, INDIA
www.acharya.ac.in, Email: hod-cse@acharya.ac.in

4. What is class? With an example illustrate how to create objects. (Syntax to


create the class)
A class is a user-defined data type. It consists of data members and member
functions, which can be accessed and used by creating an instance of that class.
It represents the set of properties or methods that are common to all objects of
one type. A class is like a blueprint for an object. Once a class has been defined we
can create any number of objects belonging to that class.
Create an Object
• In C++, an object is created from a class. We have already created the class named
MyClass, so now we can use this to create objects.
• To create an object of MyClass, specify the class name, followed by the object
name.
• To access the class attributes (myNum and myString), use the dot syntax (.) on
the object:
Create an object called "myObj" and access the attributes:
class MyClass //The class
{
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};

int main()
{
MyClass myObj; // Create an object of MyClass
// Access attributes and set values
myObj.myNum = 15;
myObj.myString = "Some text";
// Print attribute values
cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;}

Department of Computer Science and Engineering 5


Department of Computer Science and Engineering
Acharya Institute of Technology
Affiliated to VTU, Recognized by GOK and Approved by AICTE, New Delhi (Accredited by NAAC and NBA)
Acharya Dr. Sarvepalli Radhakrishnan Road, Acharya P.O., Soladevanahalli, Bangalore-560107, INDIA
www.acharya.ac.in, Email: hod-cse@acharya.ac.in

The class keyword is used to create a class called MyClass.


1.The public keyword is an access specifier, which specifies that members
(attributes and methods) of the class are accessible from outside the class.
2. Inside the class, there is an integer variable myNum and a string variable
myString.
3. When variables are declared within a class, they are called attributes.
4. At last, end the class definition with a semicolon ;.

5. What is an object? Explain with an example to create object.


• Objects are basic run-time entities in an object-oriented system. An Object
is an instance of a Class. When a class is defined, no memory is allocated but
when it is instantiated (i.e. an object is created) memory is allocated.
• An object has an identity, state, and behavior. Each object contains data and
code to manipulate the data.
• Objects can interact without having to know details of each other’s data or
code, it is sufficient to know the type of message accepted and type of
response returned by the objects.

Department of Computer Science and Engineering 6


Department of Computer Science and Engineering
Acharya Institute of Technology
Affiliated to VTU, Recognized by GOK and Approved by AICTE, New Delhi (Accredited by NAAC and NBA)
Acharya Dr. Sarvepalli Radhakrishnan Road, Acharya P.O., Soladevanahalli, Bangalore-560107, INDIA
www.acharya.ac.in, Email: hod-cse@acharya.ac.in

class ClassName
{
// Data members (attributes)
public:
// Member functions (methods)
void display()
{
cout << "Hello, World!" << endl;
}
};

int main()
{
// Creating an object of ClassName
ClassName obj; // Declare object obj of class ClassName
// Accessing member function using the object
obj.display(); // Calls the display() function of the object
return 0;
}
6. Explain the basic C++ syntax with an example?
#include<iostream>
using namespace std;
//main is where the program execution begins
int main()
{
cout<<”Hello World”; //prints Hello World
return 0;}

Department of Computer Science and Engineering 7


Department of Computer Science and Engineering
Acharya Institute of Technology
Affiliated to VTU, Recognized by GOK and Approved by AICTE, New Delhi (Accredited by NAAC and NBA)
Acharya Dr. Sarvepalli Radhakrishnan Road, Acharya P.O., Soladevanahalli, Bangalore-560107, INDIA
www.acharya.ac.in, Email: hod-cse@acharya.ac.in

Line 1: #include<iostream> is a header file library that lets us use input and output
objects like cin and cout. Header files add functionality to C++ Programs.
Line 2: using namespace std means that we can use names for objects and variables
from the standard library.
Line 3: A blank line. C++ ignores white space. But we use it to make the code more
readable.
Line 4: Another thing that always appear in a C++ program, is int main(). This is
called a function. Any code inside its curly brackets {} will be executed.
Line 5: cout (pronounced "see-out") is an object used together with the insertion
operator (<<) to output/print text. In our example it will output "Hello World!".
Line 6: return 0 ends the main function.
Line 7: Adding the closing curly bracket } is to actually end the main function.

7. What is the abstract class? What is the need of abstract class in C++? Discuss
with an example.
Abstract classes in C++ are a key concept in object-oriented programming that
provide a way to define interfaces and enforce a contract for derived classes. An
abstract class is a class that cannot be instantiated on its own and is typically used
as a base class for other classes.

Characteristics of Abstract Classes:

1. Contains Pure Virtual Functions: An abstract class contains at least one pure
virtual function. A pure virtual function is declared by assigning 0 in its declaration,
indicating that it must be overridden in derived classes.

Department of Computer Science and Engineering 8


Department of Computer Science and Engineering
Acharya Institute of Technology
Affiliated to VTU, Recognized by GOK and Approved by AICTE, New Delhi (Accredited by NAAC and NBA)
Acharya Dr. Sarvepalli Radhakrishnan Road, Acharya P.O., Soladevanahalli, Bangalore-560107, INDIA
www.acharya.ac.in, Email: hod-cse@acharya.ac.in

The syntax for declaring a pure virtual function is:

virtual void functionName() = 0;

2. Cannot be Instantiated: You cannot create objects of an abstract class. It serves


as a blueprint for derived classes, which provide specific implementations for its
pure virtual functions.

3. Enforces a Contract: Derived classes must implement all pure virtual functions
from the abstract class. This enforces a consistent interface across different derived
classes.

Department of Computer Science and Engineering 9


Department of Computer Science and Engineering
Acharya Institute of Technology
Affiliated to VTU, Recognized by GOK and Approved by AICTE, New Delhi (Accredited by NAAC and NBA)
Acharya Dr. Sarvepalli Radhakrishnan Road, Acharya P.O., Soladevanahalli, Bangalore-560107, INDIA
www.acharya.ac.in, Email: hod-cse@acharya.ac.in

Need of Abstract Class in C++:

Providing a Common Interface: An abstract class allows you to define a common


interface for all derived classes.

Enforcing a Contract on Derived Classes: An abstract class enforces that derived


classes implement specific methods.

Code Reusability: An abstract class can implement some functionality that can be
shared across derived classes.

Department of Computer Science and Engineering 10

You might also like