Oops Answers
Oops Answers
Oops Answers
Data hiding is the instruction of the data from direct access by the
program. It helps the programmer to build secure programs that cannot
be invaded by code on other parts of the program. It protects and
maintains object integrity by preventing intended or unintended changes
and intrusions
It is used to express broad concepts from which more concrete classes can
be derived. An abstract class-type object cannot be created. It cannot be
instantiated, but they can be subclassed
6 List out the advantages of new operator over malloc[] 1 1
The execution time of new is less than the malloc() function as new is a
construct, and malloc is a function.
new syntax is more readable and easier to maintain.
1. Objects
2. Classes
3. Data abstraction and encapsulation
4. Inheritance
5. Polymorphism
6. Dynamic binding
7. Message passing
1. auto
2. register
3. extern
4. static
5. mutable
PART B (6 MARKS)
1 Give classification of operators available in C++ with the help of neat and 1 2
clean diagram.
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operator
OBJECT
Object is a triangle of entity that may be exhibiting some well-defined
behavior.
These are the basic run-time entities in an object-oriented system.
They may represent a person, a place, a bank account, a table of data or
any item the program has to handle.
They may be represents userdefined data such as vectors, time and lists.
CLASSES
Class is a set of attributes and behavior shared by similar objects (or) in
simple way collection of objects of similar type is called a Class.
It holds its own data members and member functions, which can be
accessed and used by creating an instance of that class.
The entire set of data and code of an object can be made a user-defined
data type with the help of a class.
POLYMORPHISM:
Polymorphism is the ability to take more than one form.
We can use the same function name again and again with different
signatures
An operation may exhibit different behaviors in different instances.
The behavior depends upon the types of data used in the operation.
ENCAPSULATION:
It is the wrapping up of data and function into a single unit (called class).
The data is not accessible to the outside world, and only those functions
which are wrapped in the class can access it
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter an integer number: ";
cin>>num;
if(num%2==0)
cout<<num<<" is an EVEN number."<<endl;
else
cout<<num<<" is an ODD number."<<endl;
return 0;
}
OUTPUT
Enter an integer number: 6
6 is an EVEN number.
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout<<"Enter first number:";
cin>>num1;
cout<<"Enter second number:";
cin>>num2;
if(num1>num2)
{
cout<<"First number "<<num1<<" is the largest";
}
else
{
cout<<"Second number "<<num2<<" is the largest";
}
return 0;
}
OUTPUT
Enter first number:5
Enter second number:9
Second number 9 is the largest
One-Dimensional Array:
In this type of array, it stores elements in a single dimension. And, In this
array, a single specification is required to describe elements of the array.
It arranges all the elements in row wise.
Two-Dimensional Array:
In this type of array, two indexes describe each element, the first index
represents a row, and the second index represents a column.
The elements are arranged row-wise and column-wise and are i number of
rows and j number of columns.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str1[10]="Hello";
char str2[10]="World";
char str3[10];
int len;
strcpy(str3,str1);
cout<<"strcpy(str3,str1): "<<str3<<endl;
if(strcmp(str1, str2)==0)
{
cout<<"The strings are equal"<<endl;
}
else {
cout<<"The strings are not equal"<<endl;
}
len=strlen(str1);
cout<<"strlen(str1): "<<len<<endl;
return 0;
}
OUTPUT
strcpy(str3,str1): Hello
The strings are not equal
strlen(str1): 5
#include <iostream>
using namespace std;
int main() {
int x,i;
float avg = 0,y;
cout << "Enter the number of elements to calculate the average: ";
cin >> x;
cout << "Enter the elements one by one \n";
for(i = 0; i < x; i++)
{
cin >> y;
avg += y;
}
avg /= x;
cout << "\nThe average of the entered input numbers is = " << avg;
return 0;
}
OUTPUT
Enter the number of elements to calculate the average: 4
Enter the elements one by one
1
2
3
4
The average of the entered input numbers is = 2.5
2 Differentiate between call by value and call by reference. Also explain with 1 2
the suitable program
CALL BY VALUE
#include <iostream>
using namespace std;
void change (int data)
void main()
{
int data=3;
change(data);
cout<<"Value of the data:"<<data<<endl;
return 0;
}
void change(int data)
{
data=5;
}
OUTPUT
Value of the data: 3
CALL BY REFERENCE
OUTPUT
Value of X is: 100
Value of Y is: 500
3 Develop the source code to find the most occurring element in an array of 1 3
integers
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int i,j,most_count=0,count,e;
int arr[15],n;
cout<<"Enter number of array elements:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the array elements:";
cin>>arr[i];
}
for(i=0;i<n;i++)
{
count=1;
for(j=i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
count ++;
}
}
if (count>most_count)
{
most_count=count;
e=arr[i];
}
}
cout<<e<<" occurred "<<most_count<<" times which is maximum";
}
getch();
return 0;
OUTPUT
Enter the number of array elements:4
Enter array elements:6
Enter array elements:6
Enter array elements:5
Enter array elements:3
6 occurred 2 times which is maximum
UNIT –II (PART A)
1 What is the difference between local variable and data member? 2 2
A local variable is declared within the body of a function and can be used
only from the point at which it is declared to the immediately following
closing brace.
Local variable are not accessible in any another function or class.
#include <iostream>
using namespace std;
int add(int num1, int num2)
{
int sum= num1 + num2;
return sum;
}
int main() {
int x = 5;
int y = 7;
int result = add(x,y);
cout << "The sum of " << x << " and " << y << " is "<< result << endl;
return 0;
Data hiding is the instruction of the data from direct access by the
program. It helps the programmer to build secure programs that cannot
be invaded by code on other parts of the program. It protects and
maintains object integrity by preventing intended or unintended changes
and intrusions
Static members can be used to store data that needs to be shared among
all instances of the class. They can help reduce memory usage by avoiding
the creation of duplicate data for each instance of the class. It can also be
used to provide a common behaviour among all instances of the class.
6 Define Polymorphism 2 1
A friend class allows to access the private and protected members of the
class that it is declared in. Friend classes are often used to implement
tightly coupled classes that need to share private data or methods
Function overloading is used to define two functions of the same type but
with a different number of parameters with the same names. Function
overloading can also be treated as compile-time polymorphism.
Function Overloading:
Function overloading is a feature in programming languages that allows
the programmer to define multiple functions with the same name but
different parameters. The compiler determines which function to call
based on the number, types, and order of the parameters passed to the
function.
Example:
#include <iostream>
using namespace std;
void SumNum(int A, int B);
void SumNum(int A, int B, int C);
void SumNum(int A, int B, int C, int D);
int main()
{
SumNum(1,2);
SumNum(1,2,3);
SumNum(1,2,3,4);
return 0;
}
void SumNum(int A, int B)
{
cout<< endl << "SUMNUM is : "<< A+B;
}
Operator Overloading:
Example:
#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
Complex operator+(Complex const& obj)
{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << '\n'; }
};
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
}
Output
12 + i9
2 Write a program to print the area of Triangle having sides (4,5) and 2 3
(5,8) .Use constructor for defining the functions
#include <iostream>
using namespace std;
class Triangle {
private:
double length;
double height;
public:
Triangle(double len, double hgt) {
length = len;
height = hgt;
}
double calculateArea() {
return (length * height)/2;
}
};
int main() {
Triangle tri1(4, 5);
Triangle tri2(5, 8);
cout << "The Area of Triangle 1: " << tri1.calculateArea() << endl;
cout << "The Area of Triangle 2: " << tri2.calculateArea() << endl;
return 0;
}
OUTPUT
The Area of Rectangle 1: 10
The Area of Rectangle 2: 20
Constructor overloading have the same name but the different number of
arguments. This allows us to create objects using different initialization
methods depending on the arguments passed to the constructor.
#include <iostream>
using namespace std;
class Person {
private:
int age;
public:
Person() {
age = 20;
}
Person(int a) {
age = a;
}
int getAge() {
return age;
}
};
int main() {
Person person1, person2(45);
cout << "Person1 Age = " << person1.getAge() << endl;
cout << "Person2 Age = " << person2.getAge() << endl;
return 0;
}
OUTPUT
Person1 Age = 20
Person2 Age = 45
4 What are the different ways to define member functions of a class. What 2 2
is the role of scope resolution operator in the definition of member
function?
Syntax
class MyClass {
public:
inline void myFunction() {
// function definition here
}
};
Syntax:
class MyClass {
public:
void myFunction(); // declaration
};
void MyClass::myFunction() {
// function definition here
}
Default Constructor:
#include <iostream>
using namespace std;
class Employee
{
public:
Employee()
{
cout<<"Default Constructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1; //creating an object of Employee
Employee e2;
return 0;
}
Output:
Default Constructor Invoked
Default Constructor Invoked
Parameterized Constructor
#include <iostream>
using namespace std;
class Employee {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
float salary;
Employee(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) {
Employee e1 =Employee(101, "Sonoo", 890000); //creating an object
of Employee
Employee e2=Employee(102, "Nakul", 59000);
e1.display();
e2.display();
return 0;
}
Output:
#include <iostream>
using namespace std;
const float PI = 3.14159;
class Shape {
public:
float area(float radius)
{
return PI * radius * radius;
}
float area(float length, float breadth)
{
return length * breadth;
}
cout << "Area of the circle: " << shape.area(radius) << endl;
cout << "Area of the rectangle: " << shape.area(length, breadth) << endl;
cout << "Area of the square: " << shape.area(side) << endl;
return 0;
}
OUTPUT
Enter the radius of the circle: 4
Enter the length and breadth of the rectangle:4 5
Enter the side of the square: 5
Area of the circle: 50.26548
Area of the rectangle: 20
Area of the square: 25