0% found this document useful (0 votes)
4 views11 pages

Oops Programs

The document contains multiple C++ programming examples, including printing a message, calculating factorials, using structures, classes, and function overloading. It also includes a series of viva questions and answers that cover fundamental concepts in C++ such as main function, preprocessor, structures, classes, memory allocation, and object-oriented programming principles. Each program is accompanied by explanations and answers to common questions related to the concepts demonstrated.

Uploaded by

jahaanbakshi74
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Oops Programs

The document contains multiple C++ programming examples, including printing a message, calculating factorials, using structures, classes, and function overloading. It also includes a series of viva questions and answers that cover fundamental concepts in C++ such as main function, preprocessor, structures, classes, memory allocation, and object-oriented programming principles. Each program is accompanied by explanations and answers to common questions related to the concepts demonstrated.

Uploaded by

jahaanbakshi74
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Program No: 1

Aim: WAP to Print a Hello world message.

Source Code:
#include<iostream>
using namespace std;
int main ()
{
cout<<”Hello world”;
return 0;
}

Aim: WAP to Calculate factorial of a number.

Source Code:
#include<iostream>
using namespace std;
int main()
{
int n,i,f=1;
cout<<"Enter no";
cin>>n;
if(n==0)
{
cout<<"Factorial"<<f;
}
else
for(i=1;i<=n;i++)
{
f=f*i;
}
cout<<"Factorial"<<f;
return 0;
}

VIVA QUESTIONS 1

Q.1 What is the use of main() function?

Ans. The main function is a special function. Every C++ program must contain a func-
tion named main. It serves as the entry point for the program. The computer will start
running the code from the beginning of the main function.

Q.2 What is a preprocessor? How is it different from compiler?


Ans. Preprocessors are programs that process our source code before compilation. Pre -
processor programs provide preprocessor directives that tell the compiler to preprocess
the source code before compiling. All of these preprocessor directives begin with a ‘#’
(hash) symbol. The ‘#’ symbol indicates that whatever statement starts with a ‘#’ will
go to the preprocessor program to get executed.
Examples of some preprocessor directives are: #include, #define, #ifndef etc. # symbol
only provides a path to the preprocessor, and a command such as include is processed
by the preprocessor program.
Preprocessor is not a part of the compiler, but is a separate step in the compilation
process. In simple terms, a Preprocessor is just a text substitution tool and it instructs the
compiler to do required pre-processing before the actual compilation.

Q.3 What is the diffence between function declaration and function definition?

Ans. A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.

Q.4 If the function doesn’t return a value, how to declare that function?

Ans. A function that doesn't return a value is declared with return type “void”.

Q.5 What are cout and cin? Why are they used? Which library is used to include them?

Ans. cin is an object of the input stream and is used to take input from input streams like
files, console, etc. cout is an object of the output stream that is used to show output. Basi-
cally, cin is an input statement while cout is an output statement. To use cin and cout in
C++ one must include the header file iostream in the program.
Program No: 2

Aim: WAP to initialize and accessing values of data members using structure.

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

struct Point {
int x = 0; // It is Considered as Default Arguments and no Error is Raised
int y = 1;
};

int main()
{
struct Point p1;

// Accessing members of point p1


// No value is Initialized then the default value is considered. ie x=0 and y=1;
cout << "x = " << p1.x << ", y = " << p1.y<<endl;

// Initializing the value of y = 20;


p1.y = 20;
cout << "x = " << p1.x << ", y = " << p1.y;
return 0;
}

VIVA QUESTIONS 2

Q.1 What is a structure?


Ans. A structure is a user-defined data type in C/C++. A structure creates a data type that
can be used to group items of possibly different types into a single type.

Q.2 How is structure different than class?


Ans. Structures and classes differ in the following particulars: Structures are value types;
classes are reference types. A variable of a structure type contains the structure's data,
rather than containing a reference to the data as a class type does. Structures use stack
allocation; classes use heap allocation.

Q.3 How to declare a structure in C++ programming?


Ans. The struct keyword defines a structure type followed by an identifier (name of the
structure). Then inside the curly braces, you can declare one or more members (de-
clare variables inside curly braces) of that structure.

Q.4 How to define a structure variable?


Ans. Once you declare a structure person as above. You can define a structure variable as:
Person bill;

Here, a structure variable bill is defined which is of type structure Person. When
structure variable is defined, only then the required memory is allocated by the com-
piler.

Q.5 How to access members of a structure?


Ans. The members of structure variable is accessed using a dot (.) operator. Suppose, you
want to access age of structure variable bill and assign it 50 to it. You can perform
this task by using following code below:

bill.age = 50;

Q.6 Explain data member and member functions.


Ans. Data Member: These members are normal C++ variables. We can create a structure
with variables of different data types in C++.
Member Functions: These members are normal C++ functions. Along with variables,
we can also include functions inside a structure declaration.
Program No: 3

Aim: WAP to find area and volume of a room using class and objects.

Source Code:

// working of objects and class in C++ Programming


#include <iostream>
using namespace std;

// create a class
class Room {

public:
double length;
double breadth;
double height;

double calculateArea() {
return length * breadth;
}

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

int main() {

// create object of Room class


Room room1;

// assign values to data members


room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;

// calculate and display the area and volume of the room


cout << "Area of Room = " << room1.calculateArea() << endl;
cout << "Volume of Room = " << room1.calculateVolume() << endl;
return 0;
}

Aim: WAP to calculate area of square and rectangle using function overloading.

#include<iostream>
using namespace std;
void area(int);
void area(int,int);
int main()
{
area(5);
area(4,8);
return 0;
}
void area(int s)
{
int a;
a=s*s;
cout<<"the area of square is="<<a<<endl;
}
void area(int l,int b)
{
int a;
a=l*b;
cout<<"the area of rectangle is ="<<a<<endl;
}
#include<iostream>
using namespace std;
void area(int);
void area(int,int);
int main()
{
area(5);
area(4,8);
return 0;
}
void area(int s)
{
int a;
a=s*s;
cout<<"the area of square is="<<a<<endl;
}
void area(int l,int b)
{
int a;
a=l*b;
cout<<"the area of rectangle is ="<<a<<endl;
}

VIVA QUESTIONS 3

Q.1 What is class?


Ans. It is a user-defined data type, which holds its own data members and member func-
tions, which can be accessed and used by creating an instance of that class. A C++ class is
like a blueprint for an object.
For Example: Consider the Class of Cars. There may be many cars with different names
and brand but all of them will share some common properties like all of them will have 4
wheels, Speed Limit, Mileage range etc. So here, Car is the class and wheels, speed lim-
its, mileage are their properties.
class class_name{

Q.2 What are public, private and protected member in class?


Ans. The access restriction to the class members is specified by the labeled public, pri-
vate, and protected sections within the class body. The keywords public, private, and
protected are called access specifiers.
A public member is accessible from anywhere outside the class but within a program.
You can set and get the value of public variables without any member function.
A private member variable or function cannot be accessed, or even viewed from outside
the class. Only the class and friend functions can access private members. By default all
the members of a class would be private.
A protected member variable or function is very similar to a private member but it pro-
vided one additional benefit that they can be accessed in child classes which are called
derived classes.

Q.3 What is an object in C++?


An Object is an instance of a Class. When a class is defined, no memory is allocated but
when it is instantiated (i.e. an object is created) memory is allocated.
ClassName ObjectName;

Q.4 What is use of scope resolution operator in C++?


Ans. In C++, scope resolution operator is ::. It is used for following purposes.
 To access a global variable when there is a local variable with same name.
 To define a function outside a class.
 To access a class’s static variables.
 In case of multiple Inheritance
 Refer to a class inside another class.

Q.5 What is function overloading?


Ans. Function overloading is a feature of object-oriented programming where two or
more functions can have the same name but different parameters. When a function name
is overloaded with different jobs it is called Function Overloading. In Function Overload-
ing “Function” name should be the same and the arguments should be different. Function
overloading can be considered as an example of a polymorphism feature in C++.

Program No: 4
Aim: WAP to illustrate dynamic allocation and deallocation of memory using new and
delete.

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

int main ()
{
// Pointer initialization to null
int* p = NULL;

// Request memory for the variable


// using new operator
p = new(nothrow) int;
if (!p)
cout << "allocation of memory failed\n";
else
{
// Store value at allocated address
*p = 29;
cout << "Value of p: " << *p << endl;
}

// Request block of memory


// using new operator
float *r = new float(75.25);

cout << "Value of r: " << *r << endl;

// Request block of memory of size n


int n = 5;
int *q = new(nothrow) int[n];

if (!q)
cout << "allocation of memory failed\n";
else
{
for (int i = 0; i < n; i++)
q[i] = i+1;

cout << "Value store in block of memory: ";


for (int i = 0; i < n; i++)
cout << q[i] << " ";
}

// freed the allocated memory


delete p;
delete r;

// freed the block of allocated memory


delete[] q;
return 0;
}

Aim: WAP to store more than one Employee’s details with an Employee id and name
implementing array of objects

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

class Employee
{
int id;
char name[30];
public:

// Declaration of function
void getdata();

// Declaration of function
void putdata();
};

// Defining the function outside


// the class
void Employee::getdata()
{
cout << "Enter Id : ";
cin >> id;
cout << "Enter Name : ";
cin >> name;
}

// Defining the function outside


// the class
void Employee::putdata()
{
cout << id << " ";
cout << name << " ";
cout << endl;
}

// Driver code
int main()
{
// This is an array of objects having
// maximum limit of 30 Employees
Employee emp[30];
int n, i;
cout << "Enter Number of Employees - ";
cin >> n;

// Accessing the function


for(i = 0; i < n; i++)
emp[i].getdata();
cout << "Employee Data - " << endl;

// Accessing the function


for(i = 0; i < n; i++)
emp[i].putdata();
}

VIVA QUESTIONS 4

Q.1 What is new()and delete() operator?


Ans. The new operator denotes a request for memory allocation on the Free Store. If suffi-
cient memory is available, a new operator initializes the memory and returns the ad-
dress of the newly allocated and initialized memory to the pointer variable.\
pointer-variable = new data-type;
Since it is the programmer’s responsibility to deallocate dynamically allocated memory,
programmers are provided delete operator in C++ language.
delete pointer-variable;

Q.2 Is it better to use malloc () or calloc ()?


Ans. The functions malloc() and calloc() are library functions that allocate memory dynami-
cally. Dynamic means the memory is allocated during runtime (execution of the pro-
gram) from the heap segment.

Malloc() Calloc()

It is a function that creates one It is a function that assigns more than one
block of memory of a fixed size. block of memory to a single variable.
It only takes one argumemt It takes two arguments.
It is faster then calloc. It is slower than malloc()
It has high time efficiency It has low time efficiency
It is used to indicate memory alloca- It is used to indicate contiguous memory
tion allcoation

Q.3 What is the purpose of realloc( )?


Ans. The function realloc is used to resize the memory block which is allocated by malloc
or calloc before.

Q.4 What is static memory allocation and dynamic memory allocation?


Ans. When the allocation of memory performs at the compile time, then it is known as
static memory. In this, the memory is allocated for variables by the compiler. When the
memory allocation is done at the execution or run time, then it is called dynamic mem-
ory allocation.

Q.5 What do you mean by array of objects?


Ans. An array is a collection of similar data items stored at contiguous memory locations
and elements can be accessed randomly using indices of an array. They can be used
to store the collection of primitive data types such as int, float, double, char, etc of
any particular type. To add to it, an array can store derived data types such as struc -
tures, pointers, etc. When a class is defined, only the specification for the object is de-
fined; no memory or storage is allocated. To use the data and access functions defined
in the class, you need to create objects.
ClassName ObjectName[number of objects];
The Array of Objects stores objects. An array of a class type is also known as an array
of objects.

You might also like