0% found this document useful (0 votes)
3 views8 pages

C++ Programming Concepts

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)
3 views8 pages

C++ Programming Concepts

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
You are on page 1/ 8

C++ PROGRAMMING CONCEPTS EXPLAINED

A) DEFINE TERM POLYMORPHISM AND LIST THE TYPES OF


POLYMORPHISM. (2 MARKS)

Polymorphism means "many forms". It is the ability in Object-Oriented


Programming (OOP) for a function or operator to behave differently based on the
object that is using it.

The two main types of polymorphism are:

• Compile-time Polymorphism: This is resolved during compilation. Examples


are Function Overloading and Operator Overloading.
• Run-time Polymorphism: This is resolved during program execution. It is
achieved using Virtual Functions and inheritance.

B) WRITE SYNTAX FOR CREATING DERIVED CLASS. (2 MARKS)

class DerivedClass : access_specifier BaseClass {


// members of derived class
};

DerivedClass is the name of the new class.


access_specifier is either public, private, or protected. public is most commonly
used for inheritance.
BaseClass is the name of the existing class you are inheriting from.

C) DEFINE POINTER AND WRITE SYNTAX OF DECLARATION OF POINTER.


(2 MARKS)

A pointer is a special variable that stores the memory address of another variable,
instead of storing the value itself.

Syntax:

dataType *pointerName;
For example, int *ptr; declares a pointer named ptr that can hold the address of
an integer variable.

D) DEFINE TERM FILE. (2 MARKS)

A file is a container in a computer's storage system used to store data, information,


settings, or commands. In programming, files are used for permanent storage of
data, so the data is not lost when the program ends.

E) DEFINE TERM ABSTRACT CLASS. (2 MARKS)

An abstract class is a class that is designed only to be a base class for other classes.
You cannot create objects of an abstract class. It typically contains at least one pure
virtual function.

F) DESCRIBE THE TERM INPUT STREAM. (2 MARKS)

An input stream is a flow of data into a program from an input device (like a
keyboard) or from a file. It is used for reading data. In C++, cin is the predefined
input stream object that reads data from the standard input (keyboard).

G) LIST THE RULES FOR OPERATOR OVERLOADING. (2 MARKS)

• At least one operand must be a user-defined type (e.g., a class object).


• You cannot change the precedence or associativity of operators.
• You cannot create new operators; you can only overload existing ones.
• You cannot change the number of operands an operator takes (e.g., the +
operator must always have two operands).
• The operators :: (scope resolution), .* (member pointer access), . (dot), and ?:
(ternary) cannot be overloaded.
H) DEVELOP A PROGRAM TO SHOW MULTIPLE INHERITANCE USING
TWO BASE CLASSES. (4 MARKS)

#include <iostream>
using namespace std;

// First Base Class


class Father {
public:
void height() {
cout << "Tall height." << endl;
}
};

// Second Base Class


class Mother {
public:
void color() {
cout << "Fair color." << endl;
}
};

// Derived class inheriting from two base classes


class Child : public Father, public Mother {
public:
void talent() {
cout << "Smart and talented." << endl;
}
};

int main() {
Child obj;
obj.height(); // From Father class
obj.color(); // From Mother class
obj.talent(); // From Child class
return 0;
}
I) WRITE A C++ PROGRAM TO DECLARE A CLASS “BOX” HAVING DATA
MEMBER’S HEIGHT, WIDTH AND BREADTH. ACCEPT THIS INFORMATION
FOR ONE OBJECT USING POINTER TO THAT OBJECT. DISPLAY THE AREA
AND VOLUME OF THAT OBJECT. (4 MARKS)

#include <iostream>
using namespace std;

class Box {
public:
double height, width, breadth;

double calculateArea() {
return width * breadth;
}

double calculateVolume() {
return height * width * breadth;
}
};

int main() {
Box box1; // Create a Box object
Box *ptr; // Declare a pointer to a Box object
ptr = &box1; // Pointer points to the address of box1

// Accessing members using pointer with arrow (->) operator


cout << "Enter height, width, and breadth: ";
cin >> ptr->height >> ptr->width >> ptr->breadth;

cout << "Area of the box: " << ptr->calculateArea() << endl;
cout << "Volume of the box: " << ptr->calculateVolume() << endl;

return 0;
}
J) DESCRIBE ‘THIS’ POINTER IN C++ WITH A SUITABLE EXAMPLE. (4
MARKS)

The 'this' pointer is a special, hidden pointer that is automatically available inside
every non-static member function of a class. It holds the address of the current
object that is calling the function.

Example:

#include <iostream>
using namespace std;

class Student {
int rollNo;
public:
void setData(int rollNo) {
// The 'this' pointer is used to distinguish
// between the class member and the parameter with the same name.
this->rollNo = rollNo;
}
void display() {
cout << "Roll No.: " << this->rollNo << endl;
}
};

int main() {
Student s1;
s1.setData(101);
s1.display();
return 0;
}

In the setData function, this->rollNo refers to the class's member variable,


while rollNo on the right side refers to the function's parameter.

K) DIFFERENTIATE BETWEEN SEQUENTIAL ACCESS AND RANDOM


ACCESS. (4 MARKS)

A comparison of sequential and random access is shown below:


Feature Sequential Access Random Access

Data is read or written from the beginning Data can be read from or written to
Definition
in a linear sequence. any position directly.

Slower for accessing data at the end or Faster, as you can jump directly to any
Access Time
middle. location.

Analogy Like a cassette tape. Like a CD or DVD.

File
ifstream, ofstream (by default). fstream with seekg() and seekp().
Operations

L) WRITE A C++ PROGRAM TO COPY THE CONTENTS OF ONE FILE INTO


ANOTHER FILE. (4 MARKS)

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

int main() {
string line;

// Open the source file for reading


ifstream sourceFile("source.txt");
// Open the destination file for writing
ofstream destFile("destination.txt");

// Check if source file opened successfully


if (!sourceFile) {
cout << "Error opening source file!";
return 1;
}

// Read from source and write to destination line by line


while (getline(sourceFile, line)) {
destFile << line << endl;
}

cout << "File copied successfully!" << endl;

// Close the files


sourceFile.close();
destFile.close();

return 0;
}

M) COMPARE COMPILE TIME POLYMORPHISM AND RUN TIME


POLYMORPHISM (4 MARKS)

A comparison between Compile Time Polymorphism and Run Time Polymorphism


is presented:

Basis Compile Time Polymorphism Run Time Polymorphism

Other Name Static / Early Binding Dynamic / Late Binding

When
During compilation. During program execution (runtime).
Resolved

Achieved by Function Overloading Achieved by Virtual Functions and


Mechanism
and Operator Overloading. inheritance.

Speed Faster execution. Slower due to overhead.

Less flexible, as the function call is More flexible, as the function call is decided
Flexibility
fixed at compile time. at runtime based on the object.

N) EXPLAIN VIRTUAL BASE CLASS WITH EXAMPLE (4 MARKS)

A virtual base class is used in inheritance to prevent multiple copies of a


grandparent base class from appearing in a class that inherits from two parents
which themselves inherit from the same base class (the "diamond problem").

Example:

#include <iostream>
using namespace std;

// Grandparent class
class Person {
public:
void display() {
cout << "I am a Person." << endl;
}
};

// Parent class 1: Uses virtual inheritance


class Father : virtual public Person { };

// Parent class 2: Uses virtual inheritance


class Mother : virtual public Person { };

// Child class inherits from both Father and Mother


class Child : public Father, public Mother { };

int main() {
Child obj;

// Without 'virtual', this would be ambiguous.


// With 'virtual', only one copy of Person exists.
obj.display();

return 0;
}

Explanation: Without the virtual keyword, the Child class would have two
copies of the Person class (one from Father and one from Mother), causing
ambiguity. Using virtual inheritance ensures that only one copy of the Person base
class exists in the Child object.

You might also like