0% found this document useful (0 votes)
99 views19 pages

2025 07 30 20 35 03 1753887903

The document provides a comprehensive overview of C++ programming concepts, including Object-Oriented Programming (OOP), tokens, inline functions, inheritance types, friend functions, streams, function prototypes, constants, manipulators, pointers, operator overloading, polymorphism, classes, and objects. It includes practical examples of C++ programs demonstrating various features such as file handling, exception handling, templates, operator overloading, and inheritance. Additionally, it discusses the differences between virtual and pure virtual functions, along with the use of static members and friend functions.

Uploaded by

venkatbabu1808
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views19 pages

2025 07 30 20 35 03 1753887903

The document provides a comprehensive overview of C++ programming concepts, including Object-Oriented Programming (OOP), tokens, inline functions, inheritance types, friend functions, streams, function prototypes, constants, manipulators, pointers, operator overloading, polymorphism, classes, and objects. It includes practical examples of C++ programs demonstrating various features such as file handling, exception handling, templates, operator overloading, and inheritance. Additionally, it discusses the differences between virtual and pure virtual functions, along with the use of static members and friend functions.

Uploaded by

venkatbabu1808
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

c++

Part A – 4 Marks Questions

1. Define Object-Oriented Programming.


Object-Oriented Programming (OOP) is a programming paradigm based on the concept of
"objects", which can contain data in the form of fields (attributes) and code in the form of
procedures (methods). It promotes code reuse and modularity.
Key features: Encapsulation, Inheritance, Polymorphism, Abstraction.

2. What are tokens in C++?


Tokens are the smallest elements of a C++ program, recognized by the compiler. Types of tokens
include:

Keywords

Identifiers

Constants

Strings

Operators

Punctuators

3. Write the syntax of an inline function.

inline return_type function_name(parameters) {


// function body
}

Example:

inline int square(int x) {


return x * x;
}

4. List any two types of inheritance in C++.

Single Inheritance

Multiple Inheritance

5. What is the use of friend function?


A friend function can access private and protected members of a class, even though it is not a
member of that class. It helps in operator overloading and accessing private data across multiple
classes.

6. What is a stream in C++?


A stream is an abstraction that represents a device on which input and output operations are
performed.
Types:

istream for input

ostream for output

7. Define function prototype with syntax.


A function prototype declares a function before its actual definition.
Syntax:

return_type function_name(parameter_list);

8. What is a constant in C++?


A constant is a fixed value that cannot be changed during program execution.
Example:

const int MAX = 100;

9. Write a note on manipulators with an example.


Manipulators are used to format the output.
Common manipulators: endl, setw, setprecision
Example:

cout << setw(10) << 123;

10. What is a pointer? Give an example.


A pointer is a variable that holds the address of another variable.
Example:

int a = 10;
int *p = &a;

11. State any two rules of operator overloading.

At least one operand must be a user-defined type.

Existing operators cannot be newly created; only overloaded.

12. Define polymorphism.


Polymorphism allows one interface to be used for different data types or operations.
Types:

Compile-time (function overloading)


Run-time (virtual functions)

13. List any four keywords in C++.


int, float, return, class

14. Define class and object in OOP.


A class is a blueprint for creating objects.
An object is an instance of a class.
Example:

class Car {
public:
void drive() {}
};
Car myCar; // Object

15. Mention any two benefits of OOP.

Code Reusability through Inheritance

Encapsulation for better data security

16. What is the purpose of new and delete operators in C++?


new dynamically allocates memory.
delete deallocates memory.
Example:

int *ptr = new int;


delete ptr;

17. Write a short note on constructor overloading.


Constructor overloading means having multiple constructors with different parameters in the
same class.
Example:

class Example {
public:
Example() {}
Example(int x) {}
};

18. What is a file stream in C++?


File streams handle file input/output.

ifstream for reading

ofstream for writing

fstream for both


Part B – 6 Marks Questions

1. Write a C++ program to demonstrate the use of inline functions.

#include <iostream>
using namespace std;

inline int square(int x) {


return x * x;
}

int main() {
int a = 5;
cout << "Square of " << a << " is " << square(a) << endl;
return 0;
}

2. Explain array and pointer relationship with an example.


In C++, the name of an array acts as a pointer to its first element.
Example:

#include <iostream>
using namespace std;

int main() {
int arr[3] = {10, 20, 30};
int *ptr = arr;

for (int i = 0; i < 3; i++) {


cout << *(ptr + i) << " ";
}
return 0;
}

Output: 10 20 30

3. Write a program to illustrate single inheritance.

#include <iostream>
using namespace std;

class Animal {
public:
void sound() {
cout << "Animal makes sound" << endl;
}
};

class Dog : public Animal {


public:
void bark() {
cout << "Dog barks" << endl;
}
};

int main() {
Dog d;
d.sound();
d.bark();
return 0;
}

4. Discuss the use of access specifiers in a class with an example.


Access specifiers: public, private, and protected.
Example:

class Demo {
private:
int a; // accessible only within class
protected:
int b; // accessible in derived class
public:
int c; // accessible everywhere
};

5. Explain different types of control structures in C++ with examples.

Sequence: Executes statements in order.

Selection: if, else, switch

Iteration: for, while, do-while


Example:

int a = 5;
if (a > 0) cout << "Positive";
for (int i = 0; i < 3; i++) cout << i;

6. Demonstrate function templates with a simple program.

#include <iostream>
using namespace std;

template <typename T>


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

int main() {
cout << add<int>(3, 4) << endl;
cout << add<float>(2.5, 3.5) << endl;
return 0;
}

7. Write a C++ program to detect the end of a file while reading.

#include <iostream>
#include <fstream>
using namespace std;

int main() {
ifstream file("data.txt");
string line;

while (getline(file, line)) {


cout << line << endl;
}

file.close();
return 0;
}

8. Write a C++ program using function overloading to calculate the area of a circle and rectangle.

#include <iostream>
using namespace std;

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

float area(float l, float b) {


return l * b;
}

int main() {
cout << "Area of circle: " << area(5.0) << endl;
cout << "Area of rectangle: " << area(4.0, 6.0) << endl;
return 0;
}

9. Explain the use of pointer to derived class with an example.


A base class pointer can point to a derived class object.
Example:

class Base {
public:
void display() {
cout << "Base class" << endl;
}
};

class Derived : public Base {


public:
void show() {
cout << "Derived class" << endl;
}
};

int main() {
Base *bptr;
Derived d;
bptr = &d;
bptr->display(); // Valid
// bptr->show(); // Invalid
}
10. Write a program that demonstrates unary operator overloading.

#include <iostream>
using namespace std;

class Count {
int value;
public:
Count(int x) : value(x) {}

void operator++() {
++value;
}

void display() {
cout << value << endl;
}
};

int main() {
Count c(5);
++c;
c.display();
return 0;
}

Excellent! Now continuing with:

---

Part C – 10 Marks Questions

1. Write a program to read from one file and write to another using file streams.

#include <iostream>
#include <fstream>
using namespace std;

int main() {
ifstream inFile("input.txt");
ofstream outFile("output.txt");
string line;

if (!inFile || !outFile) {
cout << "Error opening files!" << endl;
return 1;
}

while (getline(inFile, line)) {


outFile << line << endl;
}

inFile.close();
outFile.close();
cout << "File copied successfully!" << endl;
return 0;
}

2. Evaluate the concept of polymorphism and its implementation using virtual functions.
Polymorphism allows one function/interface to behave differently for different classes.

Compile-time polymorphism (function/operator overloading)

Run-time polymorphism using virtual functions

Example:

#include <iostream>
using namespace std;

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

class Derived : public Base {


public:
void show() override {
cout << "Derived class show" << endl;
}
};

int main() {
Base* ptr;
Derived d;
ptr = &d;
ptr->show(); // Calls Derived's show due to virtual function
return 0;
}

3. Create a menu-driven program that demonstrates inline functions, function overloading, and
templates.

#include <iostream>
using namespace std;

inline int square(int x) {


return x * x;
}

int add(int a, int b) {


return a + b;
}

float add(float a, float b) {


return a + b;
}

template <typename T>


T maxVal(T a, T b) {
return (a > b) ? a : b;
}

int main() {
int choice;
cout << "1. Inline Function\n2. Function Overloading\n3. Template\n";
cin >> choice;

switch (choice) {
case 1:
cout << "Square of 5: " << square(5) << endl;
break;
case 2:
cout << "Int add: " << add(2, 3) << endl;
cout << "Float add: " << add(2.5f, 3.5f) << endl;
break;
case 3:
cout << "Max of 10, 20: " << maxVal(10, 20) << endl;
break;
default:
cout << "Invalid choice" << endl;
}
return 0;
}

4. Develop a class that uses static members and friend functions.

#include <iostream>
using namespace std;

class Box {
int length;
static int count;
public:
Box(int l) : length(l) { count++; }

friend void showLength(Box);


static int getCount() { return count; }
};

int Box::count = 0;

void showLength(Box b) {
cout << "Length: " << b.length << endl;
}

int main() {
Box b1(5), b2(10);
showLength(b1);
cout << "Object count: " << Box::getCount() << endl;
return 0;
}

5. Write a C++ program demonstrating exception handling for division by zero.

#include <iostream>
using namespace std;

int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;

try {
if (b == 0)
throw "Division by zero!";
cout << "Result: " << a / b << endl;
} catch (const char* msg) {
cout << "Error: " << msg << endl;
}

return 0;
}

6. Explain the various types of inheritance in C++ with appropriate examples.

Single Inheritance: One base and one derived class

Multiple Inheritance: Multiple base classes

Multilevel Inheritance: Derived from derived

Hierarchical Inheritance: One base, multiple derived

Hybrid Inheritance: Combination of above types

Example:

class A { };
class B { };
class C : public A, public B { }; // Multiple inheritance

7. Design a class for complex numbers and overload the + and - operators.

#include <iostream>
using namespace std;

class Complex {
float real, imag;
public:
Complex(float r = 0, float i = 0) : real(r), imag(i) {}

Complex operator+(Complex c) {
return Complex(real + c.real, imag + c.imag);
}

Complex operator-(Complex c) {
return Complex(real - c.real, imag - c.imag);
}

void display() {
cout << real << " + " << imag << "i" << endl;
}
};

int main() {
Complex c1(3.0, 2.0), c2(1.0, 4.0);
Complex sum = c1 + c2;
Complex diff = c1 - c2;
sum.display();
diff.display();
return 0;
}

8. Analyze the difference between virtual and pure virtual functions with code.

Virtual function: Has a body, can be overridden in derived class.

Pure virtual function: Declared by assigning = 0, must be overridden, makes a class abstract.

#include <iostream>
using namespace std;

class Base {
public:
virtual void show() {
cout << "Virtual function in Base" << endl;
}

virtual void display() = 0; // Pure virtual function


};

class Derived : public Base {


public:
void show() override {
cout << "Overridden show in Derived" << endl;
}

void display() override {


cout << "Implementation of pure virtual function" << endl;
}
};

int main() {
Derived d;
d.show();
d.display();
return 0;
}

---

9. Develop a C++ program using a class template that works for int and float types.

#include <iostream>
using namespace std;

template <class T>


class Calculator {
T a, b;
public:
Calculator(T x, T y) : a(x), b(y) {}

void display() {
cout << "Sum: " << a + b << endl;
cout << "Difference: " << a - b << endl;
}
};

int main() {
Calculator<int> intCalc(10, 5);
Calculator<float> floatCalc(2.5f, 1.2f);

intCalc.display();
floatCalc.display();
return 0;
}

---

10. Describe stream classes and file handling operations with a detailed example.

Stream classes:

ifstream – Input from file

ofstream – Output to file

fstream – Both input and output

Example:

#include <iostream>
#include <fstream>
using namespace std;

int main() {
ofstream out("test.txt");
out << "Hello, File!" << endl;
out.close();

ifstream in("test.txt");
string line;
while (getline(in, line)) {
cout << line << endl;
}
in.close();

return 0;
}
---

11. Explain the concepts of friend function with a working C++ program.

#include <iostream>
using namespace std;

class Sample {
private:
int data;
public:
Sample(int x) : data(x) {}

friend void showData(Sample s);


};

void showData(Sample s) {
cout << "Data is: " << s.data << endl;
}

int main() {
Sample obj(100);
showData(obj);
return 0;
}

---

12. Create a class for a student with static members, constructors, and a destructor.

#include <iostream>
using namespace std;

class Student {
int id;
static int count;
public:
Student(int i) {
id = i;
count++;
cout << "Student " << id << " created." << endl;
}

~Student() {
cout << "Student " << id << " destroyed." << endl;
}

static void displayCount() {


cout << "Total students: " << count << endl;
}
};
int Student::count = 0;

int main() {
Student s1(1), s2(2);
Student::displayCount();
return 0;
}

---

13. Write a program to implement multiple inheritance and explain its working.

#include <iostream>
using namespace std;

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

class B {
public:
void showB() {
cout << "Class B" << endl;
}
};

class C : public A, public B {


public:
void showC() {
cout << "Class C" << endl;
}
};

int main() {
C obj;
obj.showA();
obj.showB();
obj.showC();
return 0;
}

---

14. Compare and contrast function overloading and operator overloading.

Aspect Function Overloading Operator Overloading

Definition Same function name, different parameters Redefining operators for user-defined types
Purpose Code reusability Operator compatibility for custom types
Syntax void func(int); void func(double); Complex operator+(Complex);
Example area(int), area(float) c1 + c2 for Complex objects

---

15. Create a class template that accepts any data type and performs basic operations.

#include <iostream>
using namespace std;

template <class T>


class Operations {
T x, y;
public:
Operations(T a, T b) : x(a), y(b) {}

void add() {
cout << "Sum: " << x + y << endl;
}

void multiply() {
cout << "Product: " << x * y << endl;
}
};

int main() {
Operations<int> obj1(5, 3);
Operations<float> obj2(2.5, 1.5);

obj1.add();
obj1.multiply();
obj2.add();
obj2.multiply();

return 0;
}

---

16. Describe the concepts of pure virtual functions and abstract classes with examples.

Pure virtual function: A virtual function with = 0, must be overridden.

Abstract class: Contains at least one pure virtual function.

Example:

#include <iostream>
using namespace std;

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

class Circle : public Shape {


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

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

---

17. Write a program that demonstrates multilevel inheritance and access modes.

#include <iostream>
using namespace std;

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

class B : public A {
public:
void displayB() {
cout << "Class B" << endl;
}
};

class C : public B {
public:
void displayC() {
cout << "Class C" << endl;
}
};

int main() {
C obj;
obj.displayA();
obj.displayB();
obj.displayC();
return 0;
}

---
18. Explain constructors and destructors with a program.

#include <iostream>
using namespace std;

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

~Demo() {
cout << "Destructor called" << endl;
}
};

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

Constructor: Initializes object.

Destructor: Cleans up memory when object is destroyed.

---

19. Discuss different control structures in C++ with syntax and examples.

20. Sequential: Executes in order.

cout << "Hello" << endl;


cout << "World" << endl;

2. Selection:

if (x > 0) cout << "Positive";


else cout << "Negative";

3. Iteration:

for (int i = 0; i < 5; i++) cout << i << endl;

4. Switch:

switch (choice) {
case 1: cout << "One"; break;
default: cout << "Other";
}

---

20. Write a C++ program to overload the binary + operator for complex numbers.

#include <iostream>
using namespace std;

class Complex {
int real, imag;
public:
Complex(int r = 0, int i = 0) : real(r), imag(i) {}

Complex operator+(Complex const& obj) {


return Complex(real + obj.real, imag + obj.imag);
}

void display() {
cout << real << " + " << imag << "i" << endl;
}
};

int main() {
Complex c1(2, 3), c2(4, 5);
Complex c3 = c1 + c2;
c3.display();
return 0;
}

--

You might also like