1.Discuss cin and cout objects in C++.
In C++, cin and cout are standard input and output stream objects:
cin: (console input) is used to read user input from the keyboard.
cout: (console output) is used to display output to the screen.
Both are defined in the <iostream> header.
cin is an object of istream and uses the extraction operator >>:
int a;
cin >> a; // Takes input from user
cout is an object of ostream and uses the insertion operator <<:
cout << "Hello, World!"; // Prints to output
Features:
Can handle multiple data types.
Can be chained for multiple inputs/outputs.
Buffer-based, so they optimize I/O performance.
Support manipulators like endl for formatting.
Essential for interactive console programs.
Example:
#include <iostream>
using namespace std;
int main() {
int age; cout << "Enter your age: ";
cin >> age;
cout << "You are " << age << " years old." << endl;
return 0; }
1b. Differentiate between OOP and POP.
OOP (Object-Oriented POP (Procedure-Oriented
Programming) Programming)
Focus Objects and Classes Functions and Procedures
Accessible (global), less
Data Secure, encapsulated secure
Approac
h Bottom-up Top-down
Reuse High (inheritance) Limited
Examples C++, Java, Python C, Fortran, Pascal
1c. What are Default Arguments? Write a program in C++ to find area of circle
by declaring ‘pi’ as default argument.
Default arguments allow a function parameter to have a default value if no
argument is passed.
Example Program:
#include <iostream>
using namespace std;
float area(float r, float pi=3.14) { return pi * r * r;
} int main() {
float radius; cout << "Enter radius: "; cin >> radius;
cout << "Area = " << area(radius) << endl;
cout << "Area (pi=3.1416): " << area(radius, 3.1416) << endl;
return 0; }
2a. What is Object-Oriented Programming? Describe the characteristics of
OOPs with a real life example.
Object-Oriented Programming (OOP): It's a paradigm based on classes and
objects to model real-world entities.
Key Characteristics:
1. Class & Object:
Class: Template or blueprint.
Object: Instance of a class.
2. Encapsulation:
Bundles data and functions together.
Provides data hiding and security.
3. Abstraction:
Hides complexity, exposes only essentials.
Example: TV remote hides circuit details.
4. Inheritance:
Mechanism for a new class to use features of existing class.
Supports code reuse.
5. Polymorphism:
Same function or operator behaves differently.
Example: draw() for both Circle and Square.
6. Message Passing:
Objects communicate using functions.
Real-life Example:
A Bank system:
Classes: Account, Customer, Transaction.
Inheritance: SavingsAccount and CurrentAccount inherit from Account.
Encapsulation: Account class hides its balance data.
Polymorphism: withdraw() behaves differently in Savings and Current
account.
Advantages: Reusability, maintenance ease, data security, and real-world
modeling.
2b. Define the following with syntax and example.
i) Class in C++
A class is a user-defined data type that groups data and functions.
Syntax:
class ClassName {
};
Example:
class Car {
public:
string brand;
void display() {
cout << brand;
} };
ii) Objects in C++
Objects are instances of a class.
Syntax:
ClassName objName;
Example:
Car myCar;
myCar.brand = "Toyota";
myCar.display();
2c. Define constructor. Write the rules followed to give the prototype of
constructor. A constructor is a special function called automatically
during object creation.
Rules:
Name must be same as class name.***No return type.***Can have
default or parameterized forms.***Cannot be static or inherited.
Prototype Example:
class Person {
public: Person(); // Constructor prototype };
3a. Define Data type. Describe the user defined data types in detail.
A data type determines the kind of data a variable can hold.
User-defined Data Types:
1. Class: Groups data and methods.
Example:***class Student { string name; int roll; };
2. Struct: Groups variables of different types.
Example: ***struct Employee { int id; float salary; };
3. Union: All members share same memory.
Example: ***union Data { int i; char c; };
4. Enum: Set of named integer constants.
Example: ***enum Day {Mon, Tue, Wed};
5. Typedef: Creates aliases for data types.
Example: ***typedef int marks;
Advantages:
Enables better program structure, real-world modeling, reusability, and easier
maintenance.
3b. Write short note on any one of the following.
i) Pass by Value:
When an argument is passed by value, a copy is sent to the function. Changes
don't affect the original value.
Example:****void fun(int x) { x = 10; }
int main() {
int a = 5;
fun(a);
cout << a; // Output: 5 *** }
ii) Inline Function:
An inline function expands the code in place to reduce function call overhead.
Best for small, frequently-called functions.
Syntax:
inline int add(int a, int b) {
return a + b; }
1A. What is an abstract class in C++? How do you declare a pure virtual
function within it?
An abstract class in C++ is a class that cannot be instantiated directly and is
meant to be a base class for other classes. It contains at least one pure virtual
function.
A pure virtual function is a virtual function declared by assigning it to 0 in its
declaration.
Syntax:
class AbstractClass {
public:
virtual void display() = 0; // Pure virtual function
};
1B. Define operator overloading in C++ with syntax. (5 marks)
Operator overloading in C++ allows you to redefine the way an operator
works for user-defined types (classes/objects).
Syntax:
cpp
return_type class_name::operator op(argument_list) {
}
Example:
cpp
class Complex {
public:
int real, imag;
Complex operator + (const Complex& obj) {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
} };
1C. Write a C++ program to overload the '+' operator for a class that
concatenates two string objects.
Operator overloading allows objects to be used with operators like built-in
data types. Here, we overload the + operator to concatenate two string
objects of a user-defined class.
Complete Program:
#include <iostream>
#include <string>
using namespace std;
class MyString {
string str;
public:
MyString(string s) : str(s) {}
MyString operator + (const MyString& obj) {
return MyString(str + obj.str); }
void display() {
cout << str << endl;
} };
int main() {
MyString s1("Hello, ");
MyString s2("World!");
MyString s3 = s1 + s2; // Concatenates s1 and s2
s3.display(); // Output: Hello, World!
return 0; }
Explanation:
The operator+ is overloaded.
It combines str of the invoking object with obj.str and returns a
new MyString object.
Demonstrates concatenation of two string objects using +.
2B. Define an array. Explain array declaration and initialization in C++.
An array is a collection of elements of the same data type stored in
contiguous memory locations. Declaration:
int arr[5]; // Declares an integer array of size 5
Initialization:
int arr = {1, 2, 3, 4, 5}; // Declares and initializes array
The elements can be accessed using indices (arr, arr, ...).
3A. What are the general rules for overloading operators?
Rules for operator overloading:
1. At least one operand must be a user-defined type (class/struct).
2. Existing operators’ precedence and associativity cannot be changed.
3. Some operators can’t be overloaded (e.g., ::, ., .*, ?:, sizeof, typeid).
4. Overloading does not allow creation of new operators.
5. Unary and binary operators must follow their original signature format.
3B. Explain the concept of polymorphism in C++. Differentiate between
compile-time and runtime polymorphism.
Polymorphism:
It means "many forms". In C++, it allows objects to be treated as instances of
their parent class rather than their actual derived class. Types:
Compile-time Polymorphism: Achieved using function overloading and
operator overloading. Decisions are made at compile time.
Example: Multiple functions with same name but different
parameters.
Runtime Polymorphism: Achieved using virtual functions. Decisions are
made at runtime.
Example: Base class pointer pointing to derived class object and calling
overridden function.
3C. List and explain the types of arrays in C++ with suitable code examples.
Types of Arrays in C++:
One-dimensional Array****Two-dimensional Array****Multi-dimensional
Array****Character Array (String)****One-dimensional Array
A simple list of elements. int arr[5] = {1, 2, 3, 4, 5}; Access: arr, arr, etc.
2. Two-dimensional Array:- Matrix-like structure of rows and columns.
int matrix = {{1,2,3}, {4,5,6}};****Access: matrix, matrix, etc.
3. Multi-dimensional Array:- Arrays with more than two dimensions.
int tensor;****Used in advanced applications like 3D matrices.
4. Character Array (C-String):- Used to store strings.
char name = "Alice";****C++ also supports standard library string.
Example #include <iostream>
using namespace std;
int main() {
int arr = {10, 20, 30};
cout << "1D Array: " << arr << ", " << arr[1] << ", " << arr << endl;
int mat = {{1, 2}, {3, 4}};
cout << "2D Array: " << mat[1] << ", " << mat[1][1] << endl;
int cube[1] = {{{1,2,3},{4,5,6}}};
cout << "3D Array: " << cube[1] << endl;
char str = "Hello";
cout << "Character Array: " << str << endl;
return 0; } *****Explanation:******
Arrays are essential for storing multiple values of same type.***1D arrays
store single list, 2D arrays store matrix-like data, 3D and higher for complex
data, and char arrays are for strings.
2C. List the types of inheritance in C++ and explain single inheritance with a
programming example.
Types of Inheritance in C++:
Single inheritance****Multiple inheritance****Multilevel
inheritance****Hierarchical inheritance*****Hybrid inheritance
Single Inheritance Explanation:
Single inheritance refers to a situation where a class (derived class) inherits
from only one base class.
Example
#include <iostream>
using namespace std;
class Base { public:
void display() {
cout << "Base class function" << endl;
} };
class Derived : public Base {
public: void show() {
cout << "Derived class function" << endl; } };
int main() { Derived obj;
obj.display(); // Accessing base class function
obj.show(); // Accessing derived class function. return 0; }
Explanation:
Derived inherits from Base.
obj of type Derived can access both display() and show().
This is an example of single inheritance, as only one class (Base) is
inherited.