Name: Abdul Gani Roll No: 201DDE1184 Course: MCA Year/Sem: 2 / 3 Paper Code: MCA 305 Paper Name: (Programming in C++)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

NAME : Abdul Gani

ROLL NO : 201DDE1184
COURSE : MCA
YEAR/SEM : 2nd / 3rd
PAPER CODE : MCA 305
PAPER NAME : (PROGRAMMING IN C++)
Q-1. What is Reusability?

Concept of Reusability in C++ Using Inheritance


C++ strongly supports the concept of reusability. The C++ classes can be reused in several ways.
Once a class has been written and tested, it can be adapted by another programmer to suit their
requirements. This is basically done by creating new classes, reusing the properties of the existing
ones. The mechanism of deriving a new class from an old one is called inheritance. The old class is
referred to as the base class and the new one is called the derived class or subclass. A derived class
includes all features of the generic base class and then adds qualities specific to the derived class.

Essential properties of reusable code:

 It is easy to find and to understand


 There is a reasonable assurance that it is correct
 It requires no separation from any containing code
 It requires no changes to be used in a new program

Myths of reuse:

 Reuse will resolve the software crisis.


 All code should be reusable.
 Reusing code is always preferable to coding from scratch.
 Object-oriented languages make writing reusable code easy.

Nontechnical obstacles:

 The author of Widget must have suspected that a reusable version of Widget would be
useful.
 The author of Widget must have expected to be rewarded for writing Widget reusably
and making it available.
 Someone must maintain Widget.
 The eventual user of Widget must suspect that Widget exists and must be able to find
it.
 The eventual user of Widget must be able to obtain Widget.
 There must be no legal obstacles to reuse of Widget.
 The user of Widget must be rewarded for reusing Widget.

Technical obstacles:
 Reusable code must work in many contexts.
 We almost never know all the contexts.
 User requirements often conflict.
 We cannot provide everything everyone wants.
 The contexts change.

Q-2. What are the Header files and Library Files?

Difference between Header file and Library

Header Files : The files that tell the compiler how to call some functionality (without
knowing how the functionality actually works) are called header files. They contain the
function prototypes. They also contain Data types and constants used with the libraries. We
use #include to use these header files in programs. These files end with .h extension.

Library : Library is the place where the actual functionality is implemented i.e. they contain
function body. Libraries have mainly two categories :

 Static
 Shared or Dynamic

Static : Static libraries contains object code linked with an end user application and then they
become the part of the executable. These libraries are specifically used at compile time which
means the library should be present in correct location when user wants to compile his/her C
or C++ program. In windows they end with .lib extension and with .a for MacOS.

Shared or Dynamic : These libraries are only required at run-time i.e, user can compile
his/her code without using these libraries. In short these libraries are linked against at compile
time to resolve undefined references and then its distributed to the application so that
application can load it at run time. For example, when we open our game folders we can find
many .dll(dynamic link libraries) files. As these libraries can be shared by multiple programs,
they are also called as shared libraries.These files end with .dll or .lib extensions. In windows
they end with .dll extension.

Header File Library

They have the extension .h They have the extension .lib


They contain function declaration. They contain function definations
They are available inside “lib sub
They are available inside “include sub directory”
directory” which itself is in Turbo
which itself is in Turbo compiler.
compiler.
Library files are non human
Header files are human readable.Since they are in
readable.Since they are in the form of
the form of source code.
machine code.
Header files in our program are included by using Library files in our program are included
a command #include which is internally handle by in last stage by special software called as
pre-processor. linker.
Q-3. What are the difference between argument and parameter?

Difference between Argument and Parameter

Argument

An argument is referred to the values that are passed within a function when the function is
called. These values are generally the source of the function that require the arguments during
the process of execution. These values are assigned to the variables in the definition of the
function that is called. The type of the values passed in the function is the same as that of the
variables defined in the function definition. These are also called Actual arguments or
Actual Parameters.

Example: Suppose a sum() function is needed to be called with two numbers to add. These
two numbers are referred to as the arguments and are passed to the sum() when it called from
somewhere else.

Parameters

The parameter is referred to as the variables that are defined during a function declaration or
definition. These variables are used to receive the arguments that are passed during a function
call. These parameters within the function prototype are used during the execution of the
function for which it is defined. These are also called Formal arguments or Formal
Parameters.
Example: Suppose a Mult() function is needed to be defined to multiply two numbers. These
two numbers are referred to as the parameters and are defined while defining the function
Mult().

Argument Parameters

When a function is called, the values that The values which are defined at the time of the
are passed during the call are called as function prototype or definition of the function
arguments. are called as parameters.
These are used in function call statement to These are used in function header of the called
send value from the calling function to the function to receive the value from the
receiving function. arguments.
During the time of call each argument is Parameters are local variables which are
always assigned to the parameter in the assigned value of the arguments when the
function definition. function is called.
They are also called Actual Parameters They are also called Formal Parameters

Q-4. What is Pointer to Array? Explain with example.


Ans
Pointer to an Array
#include<stdio.h>
  
int main()
{
  int arr[5] = { 1, 2, 3, 4, 5 };
  int *ptr = arr;
  
  printf("%p\n", ptr);
  return 0;
}

In this program, we have a pointer ptr that points to the 0th element of the array. Similarly, we
can also declare a pointer that can point to whole array instead of only one element of the
array. This pointer is useful when talking about multidimensional arrays.
Syntax:

data_type (*var_name)[size_of_array];
Here ptr is pointer that can point to an array of 10 integers. Since subscript have higher
precedence than indirection, it is necessary to enclose the indirection operator and pointer
name inside parentheses. Here the type of ptr is ‘pointer to an array of 10 integers’.
Note : The pointer that points to the 0th element of array and the pointer that points to the
whole array are totally different.

Pointers and two dimensional Arrays: In a two dimensional array, we can access each
element by using two subscripts, where first subscript represents the row number and second
subscript represents the column number. The elements of 2-D array can be accessed with the
help of pointer notation also. Suppose arr is a 2-D array, we can access any element arr[i][j]
of the array using the pointer expression *(*(arr + i) + j). Now we’ll see how this expression
can be derived.
Let us take a two dimensional array arr[3][4]:

Pointers and Three Dimensional Arrays


In a three dimensional array we can access each element by using three subscripts. Let us
take a 3-D array-
int arr[2][3][2] = { {{5, 10}, {6, 11}, {7, 12}}, {{20, 30}, {21, 31}, {22, 32}} };

We can consider a three dimensional array to be an array of 2-D array i.e each element of a 3-
D array is considered to be a 2-D array. The 3-D array arr can be considered as an array
consisting of two elements where each element is a 2-D array. The name of the array arr is a
pointer to the 0th 2-D array.

Q-5. What is Inheritance ?


Ans :- C++ Inheritance

In C++, inheritance is a process in which one object acquires all the properties and behaviors
of its parent object automatically. In such way, you can reuse, extend or modify the attributes
and behaviors which are defined in other class.
In C++, the class which inherits the members of another class is called derived class and the
class whose members are inherited is called base class. The derived class is the specialized
class for the base class.

Advantage of C++ Inheritance

Code reusability: Now you can reuse the members of your parent class. So, there is no need
to define the member again. So less code is required in the class.

Types Of Inheritance

C++ supports five types of inheritance:

 Single inheritance
 Multiple inheritance
 Hierarchical inheritance
 Multilevel inheritance
 Hybrid inheritance

C++ Single Inheritance

Single inheritance is defined as the inheritance in which a derived class is inherited from the
only one base class.

C++ Multilevel Inheritance

Multilevel inheritance is a process of deriving a class from another derived class.

When one class inherits another class which is further inherited by another class, it is known as multi
level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of
all its base classes.

C++ Hierarchical Inheritance

Hierarchical inheritance is defined as the process of deriving more than one class from a base
class.

C++ Multiple Inheritance

Multiple inheritance is the process of deriving a new class that inherits the attributes from
two or more classes.

A class can also be derived from one class, which is already derived from another class.

In the following example, MyGrandChild is derived from class MyChild (which is derived
from MyClass).
C++ Hybrid Inheritance

Hybrid inheritance is a combination of more than one type of inheritance.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy