Sie sind auf Seite 1von 20

With the name of Allah Who is Most Merciful and Beneficial

Assignment

Subject

OOP in C++

Submitted To

Prof. Sidra

Submitted By

Haiqa(SE)

Superior University Sialkot

Arrays:
1

An array is a group of consecutive memory locations with same name and


type. Simple variable is a single memory location with a unique name and a type. But
an array is a collection of different memory locations. The memory locations in the array
are known as elements of array and the total number of elements in the array is called
its length.

Program:
Write a program to find sum & average of 5 numbers using array.

Output:

Multidimensional Arrays:
2

Multidimensional Array is also called as array of arrays.


Multidimensional Arrays are not limited to two indices. Multidimensional Arrays can have
three, four or more dimensions. The amount of memory needed for an array rapidly
increases with each dimensions.

Syntax& Example:
Int arr[5][7][9];
Program:
Write a program to get 12 values and show them on screen using
Multidimensional arrays.

Output:

Function Overloading:
The process of declaring multiple functions with same name
but different parameters is called function overloading. The function with same number
must differ in one of the following ways:
1. Number of Parameters.
2. Type of parameters.
3. Sequence of parameters.
Program:
Write a program to find Difference of two integers and two float numbers
overloading.

using

Output:

Structures:
A structure is a collection of multiple data types that can be referenced
with single name . It may contains similar or different data types. The data items in a
structure are called structure elements , members or fields.

Syntax:
Struct Structure_Name
{
Data_Type Identifier 1;
Data_Type Identifier 1;
:
:
};
Program:
Write a program
using structures.

to get details of employee and show them on screen

Output:

Inline Function:
C++ inline function is powerful concept that is commonly used with classes.
If a function is inline, the compiler places a copy of the code of that function
at each point where the function is called at compile time.
To inline a function, place the keyword inline before the function name and
define the function before any calls are made to the function. The compiler can
ignore the inline qualifier in case defined function is more than a line.

Program:

Output:

Constructor :
A class constructor is a special member function of a class that is executed
whenever we create new objects of that class.
A constructor will have exact same name as the class and it does not have any return
type at all, not even void. Constructors can be very useful for setting initial values for
certain member variables.
8

Program:
Write a program to find average of two numbers using constructor.

Recursion:
A programming technique in which a function calls itself is known as
recursion. A function that calls itself is called recursive function. The recursion is very
powerful technique in programming language.

Program:
Write a program that inputs two numbers and calculates factorial of number
recursively.

Output:
10

Function:
A function is a named block of code that performs some action. The
statements written in a function are executed when it is called by its name. Each
function has a unique name. Functions are the building blocks of C++ programs. The
functions are used to accomplish similar kind of tasks again and again without writing
the same code again.

Syntax:
Return-type Function-Name(Parameters)

Program:
Write a program to find power of first number raised to second using
function.
11

Output:

Classes:
12

A collection of objects with some properties and functions is known as


class. A class is used to define the characteristics of the objects. It is used as a model
for creating objects of same type.

Syntax:
Class Identifier
{
Body of Class
};
Program:
Write a program to find area of circle using classes.

13

Output:

Data Abstraction:
Data abstraction refers to, providing only essential information to the outside world and
hiding their background details, i.e., to represent the needed information in program
without presenting the details.
Data abstraction is a programming (and design) technique that relies on the separation
of interface and implementation.
Program :

14

Output:

Data Encapsulation:
15

Encapsulation is an Object Oriented Programming concept that binds together the data and
functions that manipulate the data, and that keeps both safe from outside interference and
misuse. Data encapsulation led to the important OOP concept of data hiding.

Program:

Output:

16

Access specifiers:
The public members:
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.

The private members:


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.
The protected members:
A protected member variable or function is very similar to a private member but it
provided one additional benefit that they can be accessed in child classes which are
called derived classes.

Program:

17

Output:
18

Global Variables:
Global variables are defined outside of all the functions, usually on top of the program.
The global variables will hold their value throughout the life-time of your program.
A global variable can be accessed by any function. That is, a global variable is
available for use throughout your entire program after its declaration.

Program:

Output:
19

20