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

Program 1 - Copy

Uploaded by

Mohd Arif
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program 1 - Copy

Uploaded by

Mohd Arif
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Q1) WAP in C++ to perform athematic operation with 3 integer variables.

Code:-
#include <iostream>
using namespace std;

int main()
{
int a=0,b=0,c=0;

cout<<"Enter 3 integer values for arithmetic operations :- ";


cin>>a>>b>>c;

int sum = a+b+c;


int sub = a-b-c;
int div = a/b/c;
int mul = a*b*c;

cout<<"Sum is :- "<<sum;
cout<<"\nSub is :- "<<sub;
cout<<"\nDiv is :- "<<div;
cout<<"\nMul is :- "<<mul;
return 0;
}

Output :-

Page 1 of 25
Q2) Without using 3rd variable swap the variable of 2 variables.

Code :-
#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
int x = 10, y = 5;

x = x + y;
y = x - y;
x = x - y;
cout << "After Swapping: x =" << x << ", y=" << y;

return 0;
}

Output :-

Page 2 of 25
Q3) WAP to print Fibonacci series.

Code :-
#include <iostream>
using namespace std;

int main()
{
int n1 = 0, n2 = 1, n3, i, number;
cout << "Enter the number of elements: ";
cin >> number;
for (i = 2; i < number; ++i) // loop starts from 2 because 0 and 1 are already printed
{
n3 = n1 + n2;
cout << n3 << " ";
n1 = n2;
n2 = n3;
}
return 0;
}

Output :-

Page 3 of 25
Q4) WAP to print factorial of a given number.

Code :-
#include <iostream>

using namespace std;

int main()

int i, fact = 1, number;

cout << "Enter any Number: ";

cin >> number;

for (i = 1; i <= number; i++)

fact = fact * i;

cout << "Factorial of " << number << " is: " << fact << endl;

return 0;

Output :-

Page 4 of 25
Q5) You are given a number, WAP in C++ to check whether the number is prime number
or not.

Code :-
#include <iostream>
using namespace std;

int main()
{
int num, i, m = 0, flag = 0;
cout << "Enter the Number to check Prime: ";
cin >> num;
m = num / 2;
for (i = 2; i <= m; i++)
{
if (num % i == 0)
{
cout << "Number is not Prime." << endl;
flag = 1;
break;

Page 5 of 25
} }
if (flag == 0)
cout << "Number is Prime." << endl;
return 0;
}

Output :-

Q6) WAP to print a triangle of asterisk form.

Code :-
#include <iostream>
using namespace std;

int main()
{
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for (int i = 1; i < rows; ++i)
{
for (int j = 1; j < i; ++j)
{
cout << "* ";
}
cout << "\n";
}
return 0;
Page 6 of 25
}

Output :-

Q7) WAP in C++ to perform athematic operation with 3 integer variables.

Code:-
#include <iostream>

using namespace std;

class Student

public:

int student_id, age, marks;

char student_name[20];

void student_Details()

cout << "Enter Student name :- ";

cin >> student_name;

cout << "Enter Student ID :- ";

cin >> student_id;

cout << "Enter Student Age :- ";

cin >> age;

Page 7 of 25
cout << "Enter Student Marks in Multiple Subjects :- ";

cin >> marks;

void show_Student_Details()

cout << "\nStudent name is :- " << student_name;

cout << "\nStudent ID is :- " << student_id;

cout << "\nStudent age is :- " << age;

cout << "\nStudent marks is :- " << marks;

};

int main()

Student s;

s.student_Details();

s.show_Student_Details();

};

Output :-

Page 8 of 25
Q8) Differentiate between a class and an object in C++.
A class is an entity that determines how an object will behave and what the object will contain. In other words, it is a
blueprint or a set of instruction to build a specific type of object. It provides initial values for member variables and
member functions or methods.

Example :-
class Car {
public:
string brand;
int year;

void startEngine() {
cout << "Engine started!" << endl;
}
};

An object is nothing but a self-contained component that consists of methods and properties to make a data useful.
It helps you to determines the behavior of the class.

From a programming point of view, an object can be a data structure, a variable, or a function that has a memory
location allocated. The object is designed as class hierarchies.

Example :-
Page 9 of 25
int main() {
Car myCar;
Car anotherCar;
myCar.brand = "Toyota";
myCar.year = 2022;
myCar.startEngine();
anotherCar.brand = "Honda";
anotherCar.year = 2020;
return 0;
}

Q9) Differentiate bw bottom up and top-down approach. It is stated that OOP is a


bottom-up approach justify the statement

S.
TOP-DOWN APPROACH BOTTOM-UP APPROACH
No.

In this approach We focus on In bottom-up approach, we solve


1. breaking up the problem into smaller problems and integrate it as
smaller parts. whole and complete the solution.

Mainly used by structured Mainly used by object-oriented


2. programming language such as programming language such as C++,
COBOL, Fortran, C, etc. C#, Python.

Each part is programmed


Redundancy is minimized by using
3. separately therefore contain
data encapsulation and data hiding.
redundancy.

Page 10 of 25
S.
TOP-DOWN APPROACH BOTTOM-UP APPROACH
No.

In this the communications is In this module must have


4.
less among modules. communication.

It is used in debugging, module


5. It is basically used in testing.
documentation, etc.

In top-down approach, In bottom-up approach composition


6.
decomposition takes place. takes place.

It is stated that OOPS is a bottom-up approach justify the statement.


It's because you first defines a class and its functions. Then you initialise an object of that class and calls functions as
per the need. Now, though process looks like to-bottom but the execution takes place in bottom-up approach. While
executing, execution flow find object initialisation first and then it looks up for declared class and then functions

Q10) Discuss the concept of inheritance and its types in C++.


In C++, inheritance is a fundamental concept that allows you to create a new class by reusing the properties and
behaviors of an existing class. The existing class is referred to as the "base class" or "parent class," and the new class
is called the "derived class" or "child class."

There are several types of inheritance in C++ :-


1. Single Inheritance :- In single inheritance, a class is derived from only one base class.

2. Multiple Inheritance :- In multiple inheritance, a class can be derived from more than one base class.

Page 11 of 25
3. Multilevel Inheritance :- In multilevel inheritance, a class is derived from another derived class.

4. Hierarchical Inheritance :- In hierarchical inheritance, multiple classes are derived from a single base class.

5. Hybrid Inheritance :- Hybrid inheritance is a combination of multiple types of inheritance. It can involve any
combination of single, multiple, multilevel, or hierarchical inheritance.

Q11) What do you mean by multiple inheritance?


Multiple inheritance is a feature in object-oriented programming languages that allows a class to inherit properties
and behaviors from more than one parent class. In other words, a class can be derived from more than one base
classes, and it inherits attributes and methods from all of them.

For example, let's say you have two classes: ClassA and ClassB. With multiple inheritance, you can create a new class,
let's call it ClassC, that inherits from both ClassA and ClassB. As a result, ClassC will have access to the attributes and
methods of both ClassA and ClassB.

Page 12 of 25
class A
{
// code of class A
}
class B
{
// code of class B
}
class C: public A, public B (access modifier class_name)
{
// code of the derived class
}

Q12) Why do we use inheritance? WAP a program in C++ that illustrate the use of
inheritance and its advantage.
We use inheritance in C++ for the reusability of code from the existing class. C++ strongly supports the concept of
reusability. Reusability is yet another essential feature of OOP(Object Oriented Programming).

It is always good to reuse something that already exists rather than trying to create the one that is already present, as
it saves time and increases reliability.

Page 13 of 25
We use inheritance in C++ when both the classes in the program have the same logical domain and when we want
the class to use the properties of its superclass along with its properties.

For example, there is a base class or parent class named “Animal,” and there is a child class named “Dog,” so, here
dog is an animal, so in “Dog class,” all the common properties of the “Animal” class should be there, along with its
property of dog animal.

Program to show Inheritance :-


Code :-
#include <iostream>

using namespace std;

class Animal {

public:

void eat() {

cout << "I can eat!" << endl;

};

class Dog : public Animal {

public:

void bark() {

cout << "I can bark! Woof woof!!" << endl;

};

int main() {

Dog dog1;

dog1.eat();

dog1.bark();

return 0;

}
Page 14 of 25
Output :-

Q13) Perform the same task without using any oops concepts (inheritance).

Code :-
#include <iostream>

using namespace std;

Page 15 of 25
void eat()

cout << "I can eat!" << endl;

};

void bark()

cout << "I can bark! Woof woof!!" << endl;

};

int main()

eat();

bark();

return 0;

Output :-

Q14) Show how using inheritance is having advantage over procedural approach.

a) The length of the programmes developed using OOP language is much larger than the procedural approach.
Since the programme becomes larger in size, it requires more time to be executed that leads to slower
execution of the programme.
b) We cannot apply OOP everywhere as it is not a universal language. It is applied only when it is required. It is
not suitable for all types of problems.

Page 16 of 25
c) Programmers need to have brilliant designing skill and programming skill along with proper planning because
using OOP is little bit tricky.
d) OOPs take time to get used to it. The thought process involved in object-oriented programming may not be
natural for some people.
e) Everything is treated as object in OOP so before applying it we need to have excellent thinking in terms of
objects.

Q15) What is a friend function, and how does it relate to OOP in C++?
In C++, a friend function is a function that is not a member of a class but is granted access to its private and protected
members. It can be declared inside a class with the keyword `friend` or defined outside the class like a regular
function.

Code :-
#include <iostream>

Page 17 of 25
using namespace std;

class MCA

private:

int a = 5, b = 10;

friend int sum(MCA obj);

};

int sum(MCA m)

return (m.a + m.b);

};

int main()

MCA obj1;

cout << "Sum of above numbers is :- " << sum(obj1);

return 0;

Output :-

Q16) What are virtual functions, and why are they essential in polymorphism with
example.
In C++, virtual functions play a crucial role in achieving polymorphism, a key concept in object-oriented programming.
Polymorphism allows objects of different types to be treated as objects of a common base type, simplifying code and
promoting flexibility. Virtual functions enable runtime polymorphism, where the correct function to be called is
determined at runtime based on the actual type of the object.

Key Concepts:
Page 18 of 25
1. Virtual Functions:

- A virtual function is a member function declared in a base class with the `virtual` keyword.

- It is intended to be overridden by derived classes, allowing them to provide their own implementation.

- The syntax for declaring a virtual function in the base class is:

virtual returnType functionName(parameters) = 0;

The `= 0` syntax indicates that the virtual function is a pure virtual function, and the base class becomes an
abstract class. Abstract classes cannot be instantiated; they serve as interfaces for derived classes to implement.

2. Polymorphism:

- Polymorphism allows objects of different types to be treated as objects of a common base type.

- It enables a single interface to represent various types, providing a more flexible and extensible design.

Code :-
#include <iostream>

using namespace std;

class A{

public:

void Welcome()

cout<<"Welcome to A\n";

};

class B : virtual public A

};

class C : virtual public A{

};

class D : virtual public A, virtual public B{

};

int main()

Page 19 of 25
{

D obj;

obj.Welcome();

return 0;

};

Output :-

Q17) WAP to use functions overloading and overriding.

Code :-
#include <iostream>

using namespace std;

Page 20 of 25
class Shape

public:

virtual void draw()

cout << "Drawing a shape." << endl;

};

class Circle : public Shape

public:

void draw()

cout << "Drawing a circle." << endl;

};

void print(int num)

cout << "Printing integer: " << num << endl;

void print(double num)

cout << "Printing double: " << num << endl;

int main()

Shape* shape = new Circle();

shape->draw();

delete shape;

Page 21 of 25
print(10);

print(3.14);

return 0;

Output :-

Q18) WAP to perform implicit and explicit type casting.

Code :-
#include <iostream>

using namespace std;

Page 22 of 25
int main()

// implicit

int x = 10;

char y = 'a';

x = x + y;

float z = x + 1.0;

cout << "x = " << x << endl

<< "y = " << y << endl

<< "z = " << z << endl;

// Explicit conversion from double to int

double a = 1.2;

int sum = (int)a + 1;

cout << "Sum = " << sum;

return 0;

Output :-

Q19) Explain the concept of encapsulation and how it is implemented in C++.


Encapsulation is one of the fundamental principles of object-oriented programming (OOP) and refers to the bundling
of data (attributes) and the methods (functions) that operate on that data into a single unit known as a class. It
provides a way to hide the internal details of a class and restrict access to certain components, promoting data
abstraction and information hiding.

Code :-

Page 23 of 25
#include <iostream>

using namespace std;

class Passport

private:

string country_of_origin = "United States";

public:

int passport_number;

string passport_name;

void dothis()

cout << passport_name << "'s " << country_of_origin << " passport number is " << passport_number;

};

int main()

Passport mypassport;

mypassport.passport_number = 476896564;

mypassport.passport_name = "Reginald Higgins";

mypassport.dothis();

return 0;

Output :-

Q20) How does polymorphism enhance the flexibility of C++ programs?


Polymorphism is a fundamental concept in object-oriented programming (OOP) that enhances the flexibility and
extensibility of C++ programs. It allows objects of different types to be treated as objects of a common base type,
providing a way to create code that can work with various derived types without knowing their specific
implementations. Polymorphism is achieved through two main mechanisms in C++: function overloading and
function overriding, typically involving the use of virtual functions.

Page 24 of 25
Code :-
#include <iostream>

using namespace std;

class Animal

public:

void eat()

cout << "Eating";

};

class Dog : public Animal

public:

void eat()

cout << "Eating Bread";

};

int main()

Dog d = Dog();

d.eat();

return 0;

};

Output :-

Page 25 of 25

You might also like