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

cpp

The document provides an overview of Object-Oriented Programming (OOP) concepts, including features like encapsulation, inheritance, polymorphism, and abstraction. It also explains related topics such as pure virtual functions, abstract classes, cascading I/O operators, destructors, and various inheritance types. Additionally, it covers static data members, friend functions, file opening modes, and access specifiers.
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)
4 views

cpp

The document provides an overview of Object-Oriented Programming (OOP) concepts, including features like encapsulation, inheritance, polymorphism, and abstraction. It also explains related topics such as pure virtual functions, abstract classes, cascading I/O operators, destructors, and various inheritance types. Additionally, it covers static data members, friend functions, file opening modes, and access specifiers.
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
You are on page 1/ 7

Q1 cout << "Name: " << name << ", Age: " <<

age;
a) List any four features of OOPs (Object-
Oriented Programming):

1. Encapsulation – Wrapping data and d) List the ways to define a constant:


functions together into a single unit
1. Using const keyword:
called class.
cpp
2. Inheritance – Acquiring properties
and behaviors from an existing class. CopyEdit

3. Polymorphism – Ability to take const int x = 10;


many forms, like function
2. Using #define preprocessor
overloading and overriding.
directive:
4. Abstraction – Hiding internal
cpp
implementation details and showing
only the functionality. CopyEdit

#define PI 3.14

b) Define pure virtual function: 3. Using enum:

A pure virtual function is a function that cpp


has no implementation in the base class and
CopyEdit
must be overridden in derived classes.
Syntax: enum { SIZE = 100 };

cpp

CopyEdit e) What is an abstract class?

virtual void display() = 0; An abstract class is a class that contains at


least one pure virtual function. It cannot be
instantiated. It is meant to be a base class.
c) What is cascading of I/O operator?

Cascading of I/O operator refers to using


f) Define multiple inheritance:
multiple input/output operations in a single
statement using << or >>. Multiple inheritance is a feature in C++
Example: where a class can inherit from more than
one base class.
cpp
Example:
CopyEdit
cpp Common manipulators in C++ are:

CopyEdit  endl – Inserts a new line and flushes


the output buffer.
class A { };
 setw() – Sets the width of the next
class B { };
input/output field.
class C : public A, public B { };
 setprecision() – Sets the decimal
precision of floating-point numbers.

g) Define destructor:  setfill() – Sets the fill character.

A destructor is a special member function  fixed, scientific – Sets the floating-


of a class that is executed automatically point notation.
when an object is destroyed.
Q2
Syntax:
a) Explain function overloading with
cpp
example:
CopyEdit
Function overloading allows multiple
~ClassName() { } functions with the same name but different
parameters to exist in the same scope. The
correct function is chosen based on the
h) What is 'this' pointer? number or type of arguments.
The this pointer is an implicit pointer Example:
available inside all non-static member
cpp
functions. It points to the object that
invoked the function. CopyEdit

#include <iostream>

i) What is Run-Time Polymorphism? using namespace std;

Run-Time Polymorphism occurs when a


function is overridden in a derived class and
class Print {
accessed through a base class pointer or
reference. It is implemented using virtual public:
functions.
void display(int i) {

cout << "Integer: " << i << endl;


j) Enlist manipulators in C++:
}
1. Single Inheritance – One derived
class inherits from one base class.
void display(double d) {
cpp
cout << "Double: " << d << endl;
CopyEdit
}
class A { };

class B : public A { };
void display(string s) {
2. Multiple Inheritance – One derived
cout << "String: " << s << endl;
class inherits from more than one
} base class.
}; cpp

CopyEdit

int main() { class A { };

Print obj; class B { };

obj.display(10); // Calls display(int) class C : public A, public B { };

obj.display(5.5); // Calls 3. Multilevel Inheritance – A class is


display(double) derived from a derived class.

obj.display("Hello"); // Calls cpp


display(string)
CopyEdit
return 0;
class A { };
}
class B : public A { };

class C : public B { };
b) What is inheritance? Explain types of
4. Hierarchical Inheritance – Multiple
inheritance:
classes inherit from the same base
Inheritance is the mechanism by which one class.
class (derived class) can acquire the
cpp
properties and behaviors (members) of
another class (base class). CopyEdit

Types of Inheritance: class A { };

class B : public A { };
class C : public A { }; }

5. Hybrid Inheritance – A combination };


of two or more types of inheritance.

int Counter::count = 0;
c) Explain static data members and static
member functions with example:
int main() {
Static data members are class-level
variables shared by all objects of the class. Counter c1, c2, c3;
Static member functions can be called
Counter::showCount(); // Output: Count:
using the class name and can access only
3
static data.
return 0;
Example:
}
cpp

CopyEdit
d) What is friend function? Write
#include <iostream>
characteristics of friend function:
using namespace std;
A friend function is a function that is not a
member of a class but is allowed to access
private and protected members of the class.
class Counter {
Characteristics:
private:
1. Defined outside the class but
static int count;
declared inside with friend keyword.

2. Not in the scope of the class.


public:
3. Can access private and protected
Counter() { members.

count++; 4. Not invoked using objects of the


class.
}
5. Can be a friend to more than one
class.
static void showCount() {
Example:
cout << "Count: " << count << endl;
cpp
CopyEdit 4. ios::binary – Open file in binary
mode.
class A {
cpp
int x = 10;
CopyEdit
friend void show(A); // Friend function
declaration ofstream file("data.dat", ios::binary);

}; Q5

a) Array of Objects

void show(A obj) { An array of objects is simply a collection


(array) of instances of a class. For example:
cout << "Value of x: " << obj.x;
cpp
}
CopyEdit

#include <iostream>
e) Explain use of any four file opening
modes: using namespace std;

1. ios::in – Open file for reading.

cpp class Student {

CopyEdit public:

ifstream file("data.txt", ios::in); string name;

2. ios::out – Open file for writing int age;


(overwrites existing content).

cpp
void display() {
CopyEdit
cout << "Name: " << name << ", Age: "
ofstream file("data.txt", ios::out); << age << endl;

3. ios::app – Append to the end of the }


file.
};
cpp

CopyEdit
int main() {
ofstream file("data.txt", ios::app);
Student s[2]; // Array of 2 Student objects
public:

s[0].name = "Alice"; int a; // Can be accessed from outside

s[0].age = 20; private:

int b; // Can only be accessed within


MyClass
s[1].name = "Bob";
protected:
s[1].age = 22;
int c; // Can be accessed in MyClass
and any class that inherits from it
for(int i = 0; i < 2; i++) {
};
s[i].display();

}
c) Constructor in Derived Class

When a derived class is created, it can have


return 0; its own constructor, and it can also call the
constructor of its base class using an
}
initializer list.

Example:
b) Access Specifiers
cpp
Access specifiers control the access levels of
CopyEdit
class members (variables and functions).
There are three main access specifiers: #include <iostream>
 public: Accessible from anywhere. using namespace std;

 private: Accessible only within the class Base {


class.
public:
 protected: Accessible within the
Base(int x) {
class and derived classes.
cout << "Base class constructor: " << x
Example:
<< endl;
cpp
}
CopyEdit
};
class MyClass {
class Derived : public Base {
public:

Derived(int x, int y) : Base(x) {

cout << "Derived class constructor: " <<


y << endl;

};

You might also like