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

CPP Asgn 22bai10314

Uploaded by

xalisiw650
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)
9 views

CPP Asgn 22bai10314

Uploaded by

xalisiw650
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/ 6

Name – Siddhartha Singh Malan

Reg No. – 22BAI10314

Q1) Explain the concept of polymorphism by an example in C++.

Polymorphism in C++ is the ability of a function or an object to take on many forms. There are two
types of polymorphism: compile-time (overloading) and runtime (overriding).

#include <iostream>

using namespace std;

class Base {

public:

virtual void display() {

cout << "Display of Base class" << endl;

};

class Derived : public Base {

public:

void display() override {

cout << "Display of Derived class" << endl;

};

int main() {

Base* basePtr;

Derived derivedObj;

basePtr = &derivedObj;

basePtr->display(); // Output: Display of Derived class


return 0;

Q2) Define Object-oriented programming and Explain feature of Object oriented

programming. How it is different than procedure oriented programming.

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects",


which can contain data and methods. Key features of OOP include:

Encapsulation: Bundling of data and methods that operate on the data within a single unit, e.g., a
class.

Inheritance: Mechanism by which one class can inherit properties and methods from another class.

Polymorphism: Ability of different classes to be treated as instances of the same class through a
common interface.

Abstraction: Hiding the complex implementation details and showing only the essential features of
the object.

Procedure-oriented programming (POP), on the other hand, is based on procedures or routines. The
main differences include:

Structure: POP is structured around functions and procedures, whereas OOP is structured around
objects.

Data Access: In POP, data is usually exposed and can be accessed by any function. In OOP, data is
encapsulated within objects and accessed via methods.

Reusability: OOP promotes reusability through inheritance and polymorphism, which is not
inherently supported in POP.

Modularity: OOP provides better modularity by organizing related data and functions into objects,
making the code more maintainable and scalable.

Q3 ) Classify the different statements available in C++ with examples.

C++ has various types of statements, including:

• Declaration Statements: Declare variables or constants.

int a;

const double pi = 3.14;

Expression Statements: Perform computations and assignments.


a = 5 + 3;
Compound Statements (Blocks): Group multiple statements.
{

int b = 10;

a = a + b;

Control Statements: Direct the flow of execution.

• Selection Statements: if, else, switch

if (a > 5) {

cout << "a is greater than 5";

} else {

cout << "a is not greater than 5";

switch (a) {

case 8:

cout << "a is 8";

break;

default:

cout << "a is not 8";

break;

Iteration Statements: for, while, do-while.


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

cout << i << " ";

int i = 0;

while (i < 10) {

cout << i << " ";

i++;

}
i = 0;

do {

cout << i << " ";

i++;

} while (i < 10);

Jump Statements: break, continue, return, goto.


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

if (i == 5) {

continue;

if (i == 8) {

break;

cout << i << " ";

return 0;

Q4) Write a program in C++ that display entered string into reverse order.

#include <iostream>

#include <string>

using namespace std;

int main() {

string str;

cout << "Enter a string: ";

getline(cin, str);

cout << "Reversed string: ";

for (int i = str.length() - 1; i >= 0; i--) {


cout << str[i];

cout << endl;

return 0;

Q5) Discuss the data types in C++ programming.

1. Basic Data Types


-Integer Types: `int`, `short`, `long`, `long long`, and their unsigned versions.

int a = 10;
unsigned int b = 20;

- Character Types: `char`, `signed char`, `unsigned char`, `wchar_t`.


char ch = 'A';
unsigned char uch = 255;

- Floating-point Types: `float`, `double`, `long double`.


float f = 3.14f;
double d = 3.14159;

- Boolean Type: `bool`.


bool isTrue = true;

2. Derived Data Types

- Arrays: Collection of elements of the same type.

int arr[5] = {1, 2, 3, 4, 5};


- Pointers: Store the memory address of another variable.
int* ptr;
int a = 10;
ptr = &a;

- References: Alias for another variable.


int& ref = a;

- Function Types: Define functions.


void func() {
// Function body
}

3. User-defined Data Types

- Structures: Group different data types.


struct Person {
string name;
int age;
};

- Classes: Similar to structures but with access specifiers.


class Car {
public:
string brand;
int year;
};

You might also like