Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views5 pages

Solved Assignment Ignou

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Differentiate between Object Oriented Programming approach and Structured Programming

Approach. Also explain advantages of Object Oriented Programming over Structured Programming.

Structured Object
Programming Oriented
Programming
Structured Programming is designed which Object Oriented Programming
focuses on process/ logical structure and is designed which focuses
then data required for that process. on data.
Structured programming follows top-down Object oriented programming
approach. follows bottom-up approach.
Structured Programming is also known Object Oriented Programming
as Modular Programming and a subset supports inheritance,
of procedural programming language. encapsulation,
abstraction, polymorphism,
etc.
In Structured Programming, Programs are In Object Oriented
divided into small self contained functions. Programming, Programs are
divided into small entities
called objects.
Object Oriented Programming can solve any complex programs. Structured
Programming provides less reusability, more function dependency. Object Oriented
Programming provides more reusability, less function dependency. Less abstraction and
less flexibility

(b) Explain different data types available in C++ programming language.

Data types define the type of data a variable can hold, for example an integer variable can hold
integer data, a character type variable can hold character data etc.

Data types in C++ are categorised in three groups: Built-in, user-defined and Derived.

char: For characters. Size 1 byte.

int: For integers. Size 2 bytes.

float: For single precision floating point. Size 4 bytes.

double: For double precision floating point. Size 8 bytes.

bool: For booleans, true or false.

wchar_t: Wide Character.


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

(a) if

Here, condition after evaluation will be either true or false. C if statement accepts boolean values – if
the value is true then it will execute the block of statements below it otherwise not. If we do not
provide the curly braces ‘{‘ and ‘}’ after if(condition) then by default if statement will consider the
first immediately below statement to be inside its block.
Example:
if(condition)
statement1;
statement2;
// Here if the condition is true, if block
// will consider only statement1 to be inside
// its block.

(b) while

In while loop, condition is evaluated first and if it returns true then the statements inside while
loop execute, this happens repeatedly until the condition returns false. When condition
returns false, the control comes out of loop and jumps to the next statement in the program
after while loop.
While Loop example in C++
#include <iostream>
using namespace std;
int main(){
int i=1;
/* The loop would continue to print
* the value of i until the given condition
* i<=6 returns false.
*/
while(i<=6){
cout<<"Value of variable i is: "<<i<<endl; i++;
}
}

(c) switch
Switch case statement is used when we have multiple conditions and we need to perform different action based
on the condition. When we have multiple conditions and we need to execute a block of statements when a
particular condition is satisfied. In such case either we can use lengthy if..else-if statement or switch case. The
problem with lengthy if..else-if is that it becomes complex when we have several conditions. The switch case is a
clean and efficient method of handling such scenarios.
Example of Switch Case
#include <iostream>
using namespace std;
int main(){
int num=5;
switch(num+2) {
case 1:
cout<<"Case1: Value is: "<<num<<endl;
case 2:
cout<<"Case2: Value is: "<<num<<endl;
case 3:
cout<<"Case3: Value is: "<<num<<endl;
default:
cout<<"Default: Value is: "<<num<<endl;
}
return 0;}
What is constructor? Define the class Account with all the basic attributes of a saving bank account.
Define the default constructor, parameterised constructor in Account class. Define member
functions display_balance(),for displaying the balance of account and cash_withdrawal( ) to
withdraw some amount from account. Use appropriate access control specifiers in this program.

A constructor is a special type of member function that initialises an object automatically


when it is created. Compiler identifies a given member function is a constructor by its name
and the return type. Constructor has the same name as that of the class and it does not
have any return type.

#include<iostream>
#include<stdio.h>
#include<string.h>

using namespace std;

class bank
{
int acno;
char nm[100], acctype[100];
float bal;
public:
bank(int acc_no, char *name, char *acc_type, float
balance) //Parameterized Constructor
{
acno=acc_no;
strcpy(nm, name);
strcpy(acctype, acc_type);
bal=balance;
}
void deposit();
void withdraw();
void display();
};
void bank::deposit() //depositing an amount
{
int damt1;
cout<<"\n Enter Deposit Amount = ";
cin>>damt1;
bal+=damt1;
}
void bank::withdraw() //withdrawing an amount
{
int wamt1;
cout<<"\n Enter Withdraw Amount = ";
cin>>wamt1;
if(wamt1>bal)
cout<<"\n Cannot Withdraw Amount";
bal-=wamt1;
}
void bank::display() //displaying the details
{
cout<<"\n ----------------------";
cout<<"\n Accout No. : "<<acno;
cout<<"\n Name : "<<nm;
cout<<"\n Account Type : "<<acctype;
cout<<"\n Balance : "<<bal;
}
int main()
{
int acc_no;
char name[100], acc_type[100];
float balance;
cout<<"\n Enter Details: \n";
cout<<"-----------------------";
cout<<"\n Accout No. ";
cin>>acc_no;
cout<<"\n Name : ";
cin>>name;
cout<<"\n Account Type : ";
cin>>acc_type;
cout<<"\n Balance : ";
cin>>balance;

bank b1(acc_no, name, acc_type, balance); //object is created


b1.deposit(); //
b1.withdraw(); // calling member functions
b1.display(); //
return 0;
}
Explain the following in detail, in context of C++ programming.

Access Specifiers

In C++, there are three access specifiers:

 public - members are accessible from outside the class


 private - members cannot be accessed (or viewed) from outside the
class
 protected - members cannot be accessed from outside the class,
however, they can be accessed in inherited classes. You will learn more
about Inheritance later.

Virtual Function
A C++ virtual function is a member function in the base class that you redefine in a
derived class. It is declared using the virtual keyword. It is used to tell the compiler to
perform dynamic linkage or late binding on the function. ... So, we create the pointer to the
base class that refers to all the derived objects.

Abstract Class
An abstract class is a class that is designed to be specifically used as a base class.
An abstract class contains at least one pure virtual function. You declare a pure virtual
function by using a pure specifier (= 0) in the declaration of a virtual member function in
the class declaration.

You might also like