BCS-031 ASSIGNMENT SOLUTION (2020-21)

If you have any queries please leave a message here
Your Message
×


In this ASSIGNMENT all the below PROGRAMMINGS are written and run in Turbo Dev-C++ 5.11


Q1. a) What is Object Oriented Programming? Explain concept of Object, Class and Inheritance with example of each.

Answer : - Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts :


Example of Object and Class in C++

#include<iostream>
using namespace std;

/* Define Class "NumberCheck" */
class NumberCheck
{
public:
void OddEvenCheck(int n)
{
if(n % 2 == 0)
cout << "Even Number";
else
cout << "Odd Number";
}
};

int main()
{
int number;
cout << "Enter the number : ";
cin >> number;
/* Create Object of Class "NumberCheck" */
NumberCheck ob1;
ob1.OddEvenCheck(number);
return 0;
}

Output :
Enter the number : 20
Even Number


Example of Inheritance in C++

#include<iostream>
using namespace std;

/* Base or Parent Class */
class Circle
{
public:
void CircleArea(float radius)
{
float area = 3.14 * radius * radius;
cout << "\n Area of the Circle is = " << area;
}
};

/* Derived or Child Class */
class Rectangle : public Circle
{
public:
void RectangleArea(float length, float breadth)
{
float area = length * breadth;
cout << "\n Area of the Rectangle is = " << area;
}
};

int main()
{
/* Create Object of Child Class */
Rectangle ob1;
/* Access Parent Class function using Child Class object */
ob1.CircleArea(3.5);
ob1.RectangleArea(10.0,20.0);
return 0;
}

Output :
Area of the Circle is = 38.465
Area of the Rectangle is = 200




Q1. b)Explain different operators available in C++ programming language.

Answer : - Solve it Yourself




Q1. c)Explain the use of followings in C++ programming with an example program for each.

a) for
b) ?:
c) ::
d)while

Answer : -

a) for loop - A loop is used for executing a block of statements repeatedly until a particular condition is satisfied.

for loop working process

Syntax :
for(initialization; condition; increment/decrement)
{
Statement(s);
}

Example : Find Factorial of a Number

#include<iostream>
using namespace std;

class Factorial
{
public:
void Calculation(int n)
{
int i, fact=1;
for(i=1;i<=n;i++)
{
fact = fact * i;
}
cout << "\n Answer = " << fact;
}
};

int main()
{
Factorial ob1;
ob1.Calculation(5);
return 0;
}

Output :
Answer = 120


b) ?: (Ternary Operator) - There is also a short-hand if else, which is known as the ternary operator because it consists of three operands. It can be used to replace multiple lines of code with a single line. It is often used to replace simple if else statements. The ternary operator also has return type.

Syntax :
(condition)? expressionTrue : expressionFalse;

Example : Check whether a given number is Odd or Even

#include<iostream>
using namespace std;

class NumberCheck
{
public:
void OddEven(int n)
{
(n%2 == 0)? cout << "Even Number" : cout << "Odd Number";
}
};

int main()
{
NumberCheck ob1;
ob1.OddEven(5);
return 0;
}

Output :
Odd Number


c) :: (Scope Resolution Operator) - In C++, scope resolution operator (::) is used for following purposes -

  1. To access a global variable when there is a local variable with same name

    #include<iostream>
    using namespace std;
    int n = 100; // Global Variable
    int main()
    {
    int n = 10; // Local Variable
    cout << endl << "Value of Global Variable is = " << ::n;
    cout << endl << "Value of local Variable is = " << n;
    return 0;
    }

    Output :
    Value of Global Variable is = 100
    Value of local Variable is = 10

  2. To define a function outside of a class

    #include<iostream>
    using namespace std;

    class Solution
    {
    public:
    void Addition(float a, float b); // Function Declaration
    };

    /* Function Definition outside of the Class using :: (Scope Resolution Operator) */
    void Solution::Addition(float a, float b)
    {
    cout << "Result of Addition is = " << (a + b);
    }

    int main()
    {
    Solution ob1;
    ob1.Addition(45.8, 10.2);
    return 0;
    }

    Output :
    Result of Addition is = 56

  3. To access a class’s static variables.

    #include<iostream>
    using namespace std;

    class Test
    {
    static int x;
    public:
    static int y;
    /* Local parameter "x" hides the class member "x", but we can access it using :: */
    void PrintValue(int x)
    {
    /* We can access class's static variable even if there is a local variable */
    cout << endl << "Value of Static Variable 'x' is = " << Test::x;
    cout << endl << "Value of Local Variable 'x' is = " << x;
    }
    };

    /* In C++, static members must be explicitly defined like this */
    int Test::x = 10;
    int Test::y = 20;

    int main()
    {
    Test ob1;
    ob1.PrintValue(50);
    cout << endl << "Value of Static Variable 'y' is = " << Test::y;
    return 0;
    }

    Output :
    Value of Static Variable 'x' is = 10
    Value of Local Variable 'x' is = 50
    Value of Static Variable 'y' is = 20

  4. In case of multiple Inheritance

    #include<iostream>
    using namespace std;

    class A
    {
    protected:
    int x;
    public:
    A() { x = 10; }
    };

    class B
    {
    protected:
    int x;
    public:
    B() { x = 20; }
    };

    class C: public A, public B
    {
    public:
    void PrintValue()
    {
    cout << endl << "Value of the 'x' of Class A is = " << A::x;
    cout << endl << "Value of the 'x' of Class B is = " << B::x;
    }
    };

    int main()
    {
    C ob1;
    ob1.PrintValue();
    return 0;
    }

    Output :
    Value of the 'x' of Class A is = 10
    Value of the 'x' of Class B is = 20

  5. For namespace

    #include<iostream>
    int main()
    {
    std::cout << "C++ Programming" << std::endl;
    return 0;
    }

    Output :
    C++ Programming

  6. Refer to a class inside another class

    #include<iostream>
    using namespace std;

    class Outside
    {
    public:
    int x;
    Outside() { x = 10; }
    class Inside
    {
    public:
    int x;
    static int y;
    Inside() { x = 20; }
    void Print();
    };
    };

    int Outside::Inside::y = 100;

    void Outside::Inside::Print()
    {
    cout << endl << "Inside Class";
    }

    int main()
    {
    Outside ob1;
    Outside::Inside ob2;
    ob2.Print();
    return 0;
    }

    Output :
    Inside Class


d) while loop - A while loop statement repeatedly executes a target statement as long as a given condition is true.

Syntax :
initialization;
while(condition)
{
Statement(s);
increment/decrement;
}

Example : Find Factorial of a Number

#include<iostream>
using namespace std;

class Factorial
{
public:
void Calculation(int n)
{
int i=1, fact=1;
while(i<=n)
{
fact = fact * i;
i++;
}
cout << "\n Answer = " << fact;
}
};

int main()
{
Factorial ob1;
ob1.Calculation(5);
return 0;
}

Output :
Answer = 120




Q2. a) What is access specifire ? Explain uses different types of access specifiers in C++ with the help of example programs.

Answer : - Access Specifiers in C++ are used to set the accessibility of the class members. That is, it sets some restrictions on the class members not to get directly accessed by the outside functions.

There are 3 types of Access Specifiers available in C++


private - The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.

Example of private Access Specifier

#include<iostream>
using namespace std;

class Circle
{
private:
void CircleArea(float radius)
{
float area = 3.14 * radius * radius;
cout << "Area of the Circle is = " << area;
}
};

int main()
{
Circle ob1;
ob1.CircleArea(3.5);
return 0;
}

Output :
ERROR : 'void Circle::CircleArea(float)' is private


protected - Protected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class.

Example of protected Access Specifier

#include<iostream>
using namespace std;

class Circle
{
protected:
float area;
void CircleArea(float radius)
{
area = 3.14 * radius * radius;
}
};

class FindArea : protected Circle
{
public:
void AreaCalculation()
{
CircleArea(3.5);
cout << "Area of the Circle is = " << area;
}
};

int main()
{
FindArea ob1;
ob1.AreaCalculation();
return 0;
}

Output :
Area of the Circle is = 38.465


If we use the code given below than it gives an error
int main()
{
Circle ob1;
ob1.CircleArea(3.5);
return 0;
}

Output :
ERROR : 'void Circle::CircleArea(float)' is protected


public - All the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.

Example of public Access Specifier

#include<iostream>
using namespace std;

class Circle
{
public:
void CircleArea(float radius)
{
float area = 3.14 * radius * radius;
cout << "Area of the Circle is = " << area;
}
};

int main()
{
Circle ob1;
ob1.CircleArea(3.5);
return 0;
}

Output :
Area of the Circle is = 38.465




Q2. b) Explain the following in detail, in context of C++ programming.

Answer : -

i) Friend Function

A friend function of a class is defined outside that class scope but it has the right to access all private and protected members of the class.

By using the keyword friend compiler knows the given function is a friend function.

For accessing the data, the declaration of a friend function should be done inside the body of a class starting with the keyword friend.

Characteristics of a Friend function -

Example -

#include<iostream>
using namespace std;

class Circle
{
private:
double area;

public:
void AreaCalculation(double radius)
{
area = 3.14 * radius * radius;
}
/* Declaration of Friend Function */
friend void print(Circle c);
};

/* Definition of Friend Function */
inline void print(Circle c)
{
cout << "Area of the Circle is = " << c.area;
}

int main()
{
Circle ob1;
ob1.AreaCalculation(3.5);
/* Call the Friend Function */
print(ob1);
return 0;
}

Output :
Area of the Circle is = 38.465


ii) Encapsulation and Data Hiding

Encapsulation is a process of combining data members and functions in a single unit called class. This is to prevent the access to the data directly, the access to them is provided through the functions of the class. It is one of the popular feature of Object Oriented Programming(OOPs) that helps in data hiding.

How Encapsulation is achieved in a class
To do this :

  1. Make all the data members private.
  2. Create public setter and getter functions for each data member in such a way that the set function set the value of data member and get function get the value of data member.

Example -

#include<iostream>
using namespace std;

class Circle
{
private:
double PI;
double area;

public:
Circle()
{
PI = 3.14;
area = 0.0;
}

void AreaCalculation(double radius)
{
area = PI * radius * radius;
cout << "Area of the Circle is = " << area;
}
};

int main()
{
Circle ob1;
ob1.AreaCalculation(3.5);
return 0;
}

Output :
Area of the Circle is = 38.465


iii) Abstract Class

Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class.

Characteristics of Abstract Class -

Example -

#include<iostream>
using namespace std;

class Base
{
public:
virtual void show() = 0;
};

class Derived : public Base
{
public:
virtual void show()
{
cout << "Derived Class";
}
};

int main()
{
Derived ob1;
ob1.show();
return 0;
}

Output :
Derived Class




Q3. a)What is inheritance? Explain advantages of inheritance. What are different types of inheritance in C++. Explain order of constructor calling in multilevel inheritance with the help of example program.

Answer : - Inheritance in C++ is a mechanism in which one object acquires all the properties and behaviors of a parent object.

The idea behind inheritance in C++ is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.

Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.

Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.


Advantages of Inheritance -


Different types of Inheritance -


Constructor calling in Multilevel Inheritance -

#include<iostream>
using namespace std;

class A
{
public:
A() { cout << "A's constructor called" << endl; }
};

class B : public A
{
public:
B() { cout << "B's constructor called" << endl; }
};

class C : public B
{
public:
C() { cout << "C's constructor called" << endl; }
};

int main()
{
C ob1;
return 0;
}

Output :
A's constructor called
B's constructor called
C's constructor called




Q3. b) What is polymorphism? Explain different types of polymorphism in C++ with the help of example programs.

Answer : - The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.

Real life example of polymorphism - A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behaviour in different situations. This is called polymorphism.

Polymorphism is considered as one of the important features of Object Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations.

In C++ polymorphism is mainly divided into two types :


Compile time Polymorphism - It is also known as static polymorphism. This type of polymorphism is achieved by Function Overloading or Operator Overloading.

Function Overloading - When there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments.

Example -

#include<iostream>
using namespace std;

class AreaCalculation
{
public:
void FindArea(float radius)
{
float area = 3.14 * radius * radius;
cout << "Area of the Circle is = " << area << endl;
}
void FindArea(float length, float breadth)
{
float area = length * breadth;
cout << "Area of the Rectangle is = " << area << endl;
}
};

int main()
{
AreaCalculation ob1;
ob1.FindArea(3.5);
ob1.FindArea(10.0, 20.0);
return 0;
}

Output :
Area of the Circle is = 38.465
Area of the Rectangle is = 200

Operator Overloading - C++ also provide option to overload operators. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type. For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types.

Example -

#include<iostream>
using namespace std;

class Increment
{
private:
int count;

public:
Increment(int n)
{
count=n;
}

/* Overloading Increment Operator */
void operator ++()
{
count = count + 2;
}
void Print()
{
cout << "Current Value = " << count;
}
};

int main()
{
Increment ob1(4);
++ob1;
ob1.Print();
return 0;
}

Output :
Current Value = 6


Runtime Polymorphism - This type of polymorphism is achieved by Function Overriding.

Function Overriding on the other hand occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.

Example -

#include<iostream>
using namespace std;

class Base
{
public:
void show()
{
cout << "Function of Base Class" << endl;
}
};

class Derived : public Base
{
public:
void show()
{
cout << "Function of Derived Class" << endl;
}
};

int main()
{
Derived ob1;
ob1.show();
ob1.Base::show();
return 0;
}

Output :
Function of Derived Class
Function of Base Class




Q4. a) Write a C++ program to overload ‘+’ operator to add two matrices. Make necessary assumptions require.

Answer : -

#include<iostream>
using namespace std;

class MatrixAddition
{
private:
int matrix[5][5], value, i, j;

public:
MatrixAddition(int m[5][5], int n)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
matrix[i][j] = m[i][j];
}
}
value = n;
}
/* Adding two matrices by overloading ‘+’ operator */
void operator +(MatrixAddition obj)
{
for(i=0;i<value;i++)
{
for(j=0;j<value;j++)
{
cout << "\t" << (matrix[i][j] + obj.matrix[i][j]);
}
cout << endl;
}
}
};

int main()
{
int a[5][5], b[5][5], x, y, z;
cout << "\n Enter the number of row/column present in the matrix : ";
cin >> z;
// Input Matrix A
cout << "\n Enter the element of matrix A";
for(x=0;x<z;x++)
{
for(y=0;y<z;y++)
{
cout << "\n Enter the value of A[" << x << "][" << y << "] : ";
cin >> a[x][y];
}
}
// Input Matrix B
cout << "\n Enter the element of matrix B";
for(x=0;x<z;x++)
{
for(y=0;y<z;y++)
{
cout << "\n Enter the value of B[" << x << "][" << y << "] : ";
cin >> b[x][y];
}
}
MatrixAddition ob1(a,z), ob2(b,z);
ob1 + ob2;
return 0;
}

Output :

Enter the number of Row / Column present in the Matrix : 2

Enter the Element of First Matrix
Enter the value of A[0][0] : 5

Enter the value of A[0][1] : 8

Enter the value of A[1][0] : 3

Enter the value of A[1][1] : 4

Enter the Element of Second Matrix
Enter the value of B[0][0] : 7

Enter the value of B[0][1] : 6

Enter the value of B[1][0] : 7

Enter the value of B[1][1] : 9

1214
1013




Q4. b) Explain use of different stream manipulators in C++?

Answer : - Manipulators are values that you can insert into or extract from iostreams to have special effects.

istr is an input stream.
ostr is an output stream.

Examples :

Predefined ManipulatorDescription
ostr << dec, istr >> decMakes the integer conversion base 10.
ostr << oct, istr >> octMake the integer conversion base 8.
ostr << hex, istr >> hexMakes the integer conversion base 16.
istr >> wsSkips whitespace until a non-whitespace character is found.
ostr << endlInserts a newline character ('\n') and invokes ostream::flush( ).
ostr << endsInserts a null (0) character. Useful when dealing with strstreams.
ostr << flushInvokes ostream::flush( ).




Q4. c) Write a C++ program to explain exceptions handling in C++.

Answer : - Exceptions are run-time anomalies or abnormal conditions that a program encounters during its execution. C++ provides following specialized keywords for this purpose.

try - Represents a block of code that can throw an exception.

catch - Represents a block of code that is executed when a particular exception is thrown.

throw - Used to throw an exception. Also used to list the exceptions that a function throws, but doesn’t handle itself.

#include<iostream>
using namespace std;

class Division
{
public:
float DivisionReasult(int x, int y)
{
if( y == 0 )
{
throw "Attempted to Divide by Zero !";
}
return (x/y);
}
};

int main ()
{
int a = 50, b = 0;
float k = 0.0;
Division ob1;
try
{
k = ob1.DivisionReasult(i, j);
cout << k << endl;
}
catch(const char* error)
{
cerr << endl << error;
}
return 0;
}

Output :
Attempted to Divide by Zero !




Q5. a) What is function template and class template? Write appropriate statements to create a template class for Queue data structure in C++.

Answer : -

Function Template - Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters as if they were any other regular type.

The format for declaring function templates with type parameters is :

For example, to create a template function that returns the greater one of two objects we could use :

template <class my_type>
my_type GetMax(my_type a, my_type b)
{
return (a>b ? a : b);
}

Here we have created a template function with my_type as its template parameter. This template parameter represents a type that has not yet been specified, but that can be used in the template function as if it were a regular type. As you can see, the function template GetMax returns the greater of two parameters of this still-undefined type.

To use this function template we use the following format for the function call :
function_name <type> (parameters);

For example, to call GetMax to compare two integer values of type int we can write :
int x, y, z;
z = GetMax <int> (x, y);

When the compiler encounters this call to a template function, it uses the template to automatically generate a function replacing each appearance of my_type by the type passed as the actual template parameter (int in this case) and then calls it. This process is automatically performed by the compiler and is invisible to the programmer.

Here is the entire example :

#include <iostream>
using namespace std;
template <class T>
T GetMax (T a, T b)
{
T result;
result = (a>b) ? a : b;
return (result);
}

int main ()
{
int i = 5, j = 8, k;
long x = 65, y = 42, z;
k = GetMax <int> (i, j);
z = GetMax <long> (x, y);
cout << k << endl;
cout << z << endl;
return 0;
}

In the example above we used the function template GetMax() twice. The first time with arguments of type int and the second one with arguments of type long. The compiler has instantiated and then called each time the appropriate version of the function.

In this specific case where the generic type T is used as a parameter for GetMax the compiler can find out automatically which data type has to instantiate without having to explicitly specify it within angle brackets (like we have done before specifying <int> and <long>). So we could have written instead :

int i = 5, j = 8, k;
k = GetMax(i, j);

Since both i and j are of type int, and the compiler can automatically find out that the template parameter can only be int. This implicit method produces exactly the same result :

#include <iostream>
using namespace std;
template <class T>
T GetMax (T a, T b)
{
T result;
result = (a>b) ? a : b;
return (result);
}

int main ()
{
int i = 5, j = 8, k;
long x = 65, y = 42, z;
k = GetMax(i, j);
z = GetMax(x, y);
cout << k << endl;
cout << z << endl;
return 0;
}

Notice how in this case, we called our function template GetMax() without explicitly specifying the type between angle-brackets <>. The compiler automatically determines what type is needed on each call.

Because our template function includes only one template parameter (class T) and the function template itself accepts two parameters, both of this T type, we cannot call our function template with two objects of different types as arguments :

int i = 5;
long j = 8, k;
k = GetMax(i, j);

This would not be correct, since our GetMax function template expects two arguments of the same type. In this call we use objects of two different types.

We can also define function templates that accept more than one type parameter, simply by specifying more template parameters between the angle brackets. For example :

template <class T, class U>
T GetMin (T a, U b)
{
return (a>b ? a : b);
}

In this case, our function template GetMin() accepts two parameters of different types and returns an object of the same type as the first parameter (T) that is passed.

int i = 5, k;
long j = 8;
k = GetMax <int, long> (i, j);

or simply :
k = GetMax(i, j);


Class Template - We also have the possibility to write class templates, so that a class can have members that use template parameters as types. For example :

template <class T>
class maximum
{
T values[2];

public:
maximum(T first, T second)
{
values[0] = first;
values[1] = second;
}
};

The only member function in the previous class template has been defined inline within the class declaration itself. In case that we define a function member outside the declaration of the class template, we must always precede that definition with the template < . . . > prefix :

#include <iostream>
using namespace std;

template <class T>
class mypair
{
T values[2];

public:
maximum(T first, T second)
{
values[0] = first;
values[1] = second;
}
T GetMax ();
}

template <class T>
T maximum::GetMax()
{
T max;
max = (values[0] > values[1]) ? a : b;
return max;
}

int main()
{
maximum ob1(100, 75);
cout << ob1.GetMax();
return 0;
}


Template Class for Queue Data Structure -

#include <iostream>
using namespace std;

/* Define default capacity of stack */
#define SIZE 4

/* Class for queue */
template <class T>
class queue
{
private:
T *array;
T front;
T rear;

public:
queue();
void insert(T value);
T removeData();
};

/* Constructor to initialize queue */
template <class T>
queue<T>::queue()
{
array = new T[SIZE];
front = 0;
rear = -1;
}

/* Insert element into the queue */
template <class T>
void queue<T>::insert(T value)
{
/* Check whether the queue is full or not */
if(rear == (SIZE - 1))
{
cout << "Queue Overflow" << endl;
}
else
{
cout << "Inserting " << value << endl;
array[++rear] = value;
}
}

/* Delete element from the queue */
template <class T>
T queue<T>::removeData()
{
T value = 0;
/* check whether the queue is empty or not */
if(rear < front)
{
cout << "Queue UnderFlow" << endl;
}
else
{
value = array[front];
front++;
}
return value;
}

// main function
int main()
{
int retn;
queue obj;
obj.insert(10);
obj.insert(20);
retn = obj.removeData();
cout << "Removing " << retn << endl;
retn = obj.removeData();
cout << "Removing " << retn << endl;
obj.insert(30);
retn = obj.removeData();
cout << "Removing " << retn << endl;
obj.insert(40);
obj.insert(50);
return 0;
}

Output :
Inserting 10
Inserting 20
Removing 10
Removing 20
Inserting 30
Removing 30
Inserting 40
Queue Overflow




Q5. b) What is function overriding, explain with examples. Also explain advantages of function overriding.

Answer : - Function overriding is a feature that allows us to have a same function in child class which is already present in the parent class. A child class inherits the data members and member functions of parent class, but when you want to override a functionality in the child class then you can use function overriding.

Example

#include<iostream>
using namespace std;

class Base
{
public:
void show()
{
cout << "Function of Base Class" << endl;
}
};

class Derived : public Base
{
public:
void show()
{
cout << "Function of Derived Class" << endl;
}
};

int main()
{
Derived ob1;
ob1.show();
ob1.Base::show();
return 0;
}

Output :
Function of Derived Class
Function of Base Class


Advantages of Function Overriding




Q5. c) Write C++ program to create an Account class to manage saving bank account activities. Make necessary assumptions.

Answer : -

#include<iostream>
using namespace std;

class Account
{
public:
long AccountNumber;
double Balance;
};

class Transaction : public Account
{
private:
Account ob[50];
int Position;

public:
Transaction()
{
Position = 0;
}

void SetValue(long AccountNo, double Amount)
{
int i, count=0;
/* Check whether the given Account Number present in the Database or not */
for(i=0;i<Position;i++)
{
if(ob[i].AccountNumber == AccountNo)
{
cout << endl << "Account Already Present in the Database";
count = 1;
break;
}
}
/* If the given Account not found in the Database then Add this Account */
if(count == 0)
{
ob[Position].AccountNumber = AccountNo;
ob[Position].Balance = Amount;
Position++;
cout << endl << "Account Successfully Added in the Database";
}
}

void MoneyTransfer(long SenderAccount, long ReceiverAccount, double Amount)
{
int i, j, count=0;
for(i=0;i<Position;i++)
{
/* Check whether the Sender Account present in the Database or not */
if(ob[i].AccountNumber == SenderAccount)
{
for(j=0;j<Position;j++)
{
/* Check whether the Receiver Account present in the Database or not */
if(ob[j].AccountNumber == ReceiverAccount)
{
ob[i].Balance = ob[i].Balance - Amount;
ob[j].Balance = ob[j].Balance + Amount;
count = 1;
cout << endl << "Transaction Successfully Completed";
break;
}
}
break;
}
}
if(count == 0)
{
cout << endl << "Account not Present in the Database";
}
}

void DisplayBalance(long AccountNo)
{
int i, count=0;
for(i=0;i<Position;i++)
{
/* If the given Account present in the Database then display the Balance */
if(ob[i].AccountNumber == AccountNo)
{
cout << endl << "Balance = " << ob[i].Balance;
count = 1;
break;
}
}
if(count == 0)
{
cout << endl << "Account not Present in the Database";
}
}
};

int main()
{
Transaction obj;
obj.SetValue(1001, 50500.68);
obj.SetValue(1002, 75825.00);
obj.SetValue(1003, 20900.25);
obj.SetValue(1003, 20900.25);
obj.SetValue(1004, 88050.72);
obj.MoneyTransfer(1004, 1002, 3000.00);
obj.DisplayBalance(1004);
obj.DisplayBalance(1002);
return 0;
}

Output :
Account Successfully Added in the Database
Account Successfully Added in the Database
Account Successfully Added in the Database
Account Already Present in the Database
Account Successfully Added in the Database
Transaction Successfully Completed
Balance = 85050.7
Balance = 78825



ABOUT US

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.


MORE TOPIC


Windows Command

UNIX Command

IGNOU Assignment Solution

IGNOU Question Paper Solution


WHAT WE DO


Website Devlopment

Training

Home Learning

Provide BCA, MCA Projects

Provide Assignment & Question Paper Solution


CONTACT US


Follow Us