Program 1 - Copy
Program 1 - Copy
Code:-
#include <iostream>
using namespace std;
int main()
{
int a=0,b=0,c=0;
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>
int main()
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 :-
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 :-
Code:-
#include <iostream>
class Student
public:
char student_name[20];
void student_Details()
Page 7 of 25
cout << "Enter Student Marks in Multiple Subjects :- ";
void show_Student_Details()
};
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;
}
S.
TOP-DOWN APPROACH BOTTOM-UP APPROACH
No.
Page 10 of 25
S.
TOP-DOWN APPROACH BOTTOM-UP APPROACH
No.
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.
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.
class Animal {
public:
void eat() {
};
public:
void bark() {
};
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>
Page 15 of 25
void eat()
};
void bark()
};
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;
};
int sum(MCA m)
};
int main()
MCA 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:
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>
class A{
public:
void Welcome()
cout<<"Welcome to A\n";
};
};
};
};
int main()
Page 19 of 25
{
D obj;
obj.Welcome();
return 0;
};
Output :-
Code :-
#include <iostream>
Page 20 of 25
class Shape
public:
};
public:
void draw()
};
int main()
shape->draw();
delete shape;
Page 21 of 25
print(10);
print(3.14);
return 0;
Output :-
Code :-
#include <iostream>
Page 22 of 25
int main()
// implicit
int x = 10;
char y = 'a';
x = x + y;
float z = x + 1.0;
double a = 1.2;
return 0;
Output :-
Code :-
Page 23 of 25
#include <iostream>
class Passport
private:
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.dothis();
return 0;
Output :-
Page 24 of 25
Code :-
#include <iostream>
class Animal
public:
void eat()
};
public:
void eat()
};
int main()
Dog d = Dog();
d.eat();
return 0;
};
Output :-
Page 25 of 25