You are on page 1of 15

PIXELES CLASSES BCA & MCA (IGNOU)

Course Code : BCS-031


Course Title : Programming in C++
Assignment Number : BCA (III)/031/Assignment/2015
October, 2015 (For July 2015 Session) ,April, 2016 (For January 2016 Session)
Question1.
(a) Diff. b/w object oriented programming approach and structured programming PIXELES
CLASSES
approach. Also explain which approach of programming is better in these two
Page | 1
approaches and why?
Ans:-
OBJECT ORIENTED PROGRAMMING:-
 Object oriented programming is designed, which focus on data.
 It is bottom up approach.
 It supports inheritance, encapsulation, polymorphism and abstraction
 In object oriented programming, program are divided into entities and object.
 It is more secure and having data hiding.
 It can solve any complex program.
 More flexibility.
STRUCTURED ORIENTED PROGRAMMING:-
 It is designed which focus on process, logical and structure
 It is top down approach.
 It is also known as procedural programming and modular programming.
 In this, program is divided into small self contained function.
 It is less secure is no way of data hiding.
 It can solve modularity complex program.
 Less flexibility
Object oriented programming is better than structured programming.
 Reusability of code.
 Fewer complexes.
 More flexible.
 Avoid confusing features.
(b) Explain different operators available in c++ programming:
Ans:-
 Arithmetical operators
 Relational operators
 Logical operators
 Bitwise operators
 Special operators
Arithmetical Operators:-
This operator performs an arithmetic (numeric) operation such as +,-,*, / or % is called
arithmetic operator. It requires two or more operands. So these operators are called binary
operators.
Operator Meaning Example Answer
+ Addition 6+3 9

www.pixelesindia.com
PIXELES CLASSES BCA & MCA (IGNOU)
- Subtraction 6-3 3
* Multiplication 6*3 18
/ Division 6/3 2
% Modulo 5%2 1
Relational Operators:- This operator is used to check the relation between two values .All
relational operators are binary operators and it require two operands. When relation is false then PIXELES
CLASSES
it returns zero otherwise non zero.
Page | 2
Operator Meaning Example
== Equal to 3==3
!= Not equal to 3!=5
> Greater than 4>2
< Less than 3<5
>= Greater than equal to 4>=4
<= Less than Equal to 7<=7

LOGICAL OPERATORS:- In logical operator meanly we consider about three oprators like
&&,||or!.
Operators Meaning
&& Logical AND
|| Logical OR
! Logical NOT

The logical operators (&&) and (||) are used for evaluating two operators to obtain a single
relational result. the operator && corresponds with Boolean logical operation AND. The
operation results true if two operands are true otherwise false.
The operator || corresponds with Boolean logical operation OR. Its result is true if either one of
its two operands is true, and being false when both operands are false.
The (!) operator performs the Boolean operation NOT. It has only one operand, located at its
right, It does it to reverse the value of it, producing false if its operand is true and true if its
operand is false.
EXAMPLE FOR && OPERATORS
((4==4) && (2>3)) //evaluates to false (true && false)
EXAMPLE FOR || OPERATORS
((4==4) || (2>3)) // Evaluates to true (true || false)
EXAMPLE FOR ! OPERATORS:-
! (4==4) //Evaluates to false because the expression at its right (4==4) is true.
! (5<=4) //Evaluates to true because (5<=4) would be false.

BITWISE OPERATORS:-Bitwise operators are used to modify the bits of the binary variables.
Operators equivalent Description
| OR Bitwise inclusive OR

~ NOT Unary complement

www.pixelesindia.com
PIXELES CLASSES BCA & MCA (IGNOU)
<< SHL Left shift
>> SHR Right shift
& AND Bitwise and

SPECIAL OPERATORS: - C++ programming supports some special operators. These are
increment (++) and decrement (--) operator, size of operator, comma operator etc. PIXELES
CLASSES
 Sizeof (char) returns 1
Page | 3
 Sizeof (int) returns 2
 Sizeof (float) returns 4
In the above statements, comma is used as a separator between the two statements.

(c) Explain the use of the following available in c++ programming with an example.
I. If
II. For
III. Switch
Ans:-
I. If statement:-
If statement is used to check the condition between two operands. If
condition is true statement is executed otherwise not executed.
Syntax of if statement
If (expression)
{
Statements;
}
EXAMPLE:-
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
a=4;
b=3;
if (a<b)
cout<<”a is less than b”;
}
II. For:- repeat the statement upto given condition..
Syntax:-
for (init; condition; increment)
{
statements;
}
example:-
#include <iostream.h>
#include<conio.h>

www.pixelesindia.com
PIXELES CLASSES BCA & MCA (IGNOU)
void main
{
int i;
for (i=1; i <10; i++)
{
cout<< i<<”pixels india .com” PIXELES
} CLASSES
} Page | 4
III. Switch: - It is used for multiple branch selection. In this expression is condition that
is being evaluated .If case 1 condition is true, first case body is executed, otherwise
case 2 is checked and so on…if none of case expression is true then the value of
default case body is executed.
EXAMPLE:-
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
cout<<”enter the number”;
cin>>num;
switch(num);
case 1:cout<<”bca”;
break;
case 2: cout<<”mca”;
break;
default:
cout<<”pixels india.com visit must be one time”;
}}
Question2.
(a) What is constructor? Define the class Account with all the basic attributes of a
saving bank account. Define the default constructor, parameterized constructor, member
function display_ balance () for displaying the balance of account use appropriate access
control specifies in this program.
ANS:-
Constructor
 Constructor is a special member function having same name of class.
 It cannot return a value even void.
 It can be parameterized.
 It can be overloaded.
 It is called automatically when create an object.
Default constructor: - A constructor with arguments or all default arguments is called
default constructor. If a constructor is not declare in class then compiler define it implicitly is
also called default constructor.
Class account

www.pixelesindia.com
PIXELES CLASSES BCA & MCA (IGNOU)
{
account() //default constructor
account(int a=3, int b=4) //default constructor
}
Parameterized constructor
A constructor has arguments is called parameterized constructor. PIXELES
CLASSES
#include<iostream.h> Page | 5
#include <conio.h>
#include<string.h>
class account
{
int acno;
float bal,irate;
char name[40];
public:
account (int a,float b,float c,char n[])
{
acno=a;
bal=b;
irate=c;
strcpy(name,n);
}
void display_balance ()
{
cout<<"\nAccount Number:\t"<<acno;
cout<<"\nAccount Holder:\t"<<name;
cout<<"\nBalance :\t"<<bal;
cout<<"\nRate of interest:\t"<<irate;
}
};
void main()
{
clrscr();
account ot(101,1000,4,"PIXELES");
ot.display_balance();
getch ();
}

(b)Explain the following in detail,in context of c++ programming.


I. Abstraction and datahiding.
II. Virtual function
III. Friend function
Ans:-

www.pixelesindia.com
PIXELES CLASSES BCA & MCA (IGNOU)
I. Abstraction and data hiding:-
It is a process to hide the information (background details) and focus on essential information.
This means that it concern with required information .That is used to solve the problem. In data
hiding, all details of object are hide that do not contribute to its essential characteristics.
II. Virtual function:-
It is a member of class that is decided by compiler on run time. This means that function of base PIXELES
class can be overridden by the function of derived class. Basically, it is used to implement run CLASSES
time polymorphism. It is declare with virtual keyword. Page | 6
III. Friend function:-.
 It is declare with friend keyword.
 It may be friends of two or more class.
 It can be a member either class;
 It does not care either it is declare with private or public;
 Generally it is access object as an argument.
Quesion 3.
(a) What is Inheritance? What is different type of inheritance? Explain how
constructor is used in inheritance with the help of c++ program.
Ans:
Inheritance is a process to create a new class from existing class or classes. Created new class
is called sub class or child class or derived class and old class is known as parent class or
super class or base class.
Advantage:
 Code reusability
 Function overriding
 Save compilation and programmer time
 Increase modularity
There are Five types of Inheritance:-
Single inheritance: - A class is derived from a Base class is called single inheritance class.
Suppose Class ‘A’ is a base class and Class ‘B’ is derived from class ‘A’ then class ‘A’ is a
super class and class ‘B’ is a sub class.

A B

Syntax:-
Class A
{
….
…..
};
Class B: public A
{
….

www.pixelesindia.com
PIXELES CLASSES BCA & MCA (IGNOU)
}
Multiple inheritances
A class is derived from more than one classes is called multiple inheritance. Here Class ‘A’ and
Class ‘B’ are Base Classes and Class ‘C’ is derived from these two Base classes.

A B
PIXELES
CLASSES
Page | 7

Multi-Level Inheritance
Class ‘B’ is derived from class ‘A’ and class ‘C’ is derived from Class ‘B’ and so on … is called
Multi level inheritance.

Hierarchal Inheritance: - Super class and sub classes are arranged in tree structure and
parent- child format.
A

B C D

Hybrid inheritance:-
It is combination of hierarchical and multiple inheritances.

A A

www.pixelesindia.com
PIXELES CLASSES BCA & MCA (IGNOU)
Constructor can be inherited in derived class.
Example
#include<iostream.h>
#include<conio.h>
class pix PIXELES
{ CLASSES
private : Page | 8
int a,b,c;
public:
pix(int x)
{
a=x;
Branches & Contacts Details
}
void disp() Uttam Nagar:-WZ-B7, Old Pankha Nangloi:-Plot. No-19, Ext- 2A,
oppBanke-Bihari, Talabwali Road,
{ Road (Opp. Primary School), Near
Nangloi, Delhi-41
cout<<"value of a="<<a; East Metro Station, Uttam Nagar,
} New Delhi-59
};
class xyz:public pix
{
Ph: 9213327975, 9716339580
int b; 8750321695
public: pixeles@rediffmail.com, web: www.pixelesindia.com
xyz(int x, int y):pix(x)
{
b=y;
}
void show()
{
cout<<"values of b="<<b;
}
};
void main()
{
clrscr();
xyz p(4,5);
p.disp();
p.show();
getch();
}

www.pixelesindia.com
PIXELES CLASSES BCA & MCA (IGNOU)
(b) Write a program to overload ‘-’ operator so that it return the different in length of two
string (note: if s1 and s2 are two string then s1-s2or s2-s1 should given the difference in
length of s1 and s2).

#include<iostream.h>
#include<string.h> PIXELES
#include<stdio.h> CLASSES
#include<conio.h> Page | 9
class substring
We are teaching IGNOU’s BCA & MCA Students
{
private:
char *p;
Why join us?
int ln;
public:  Regular Classes
substring()
{  BCA & MCA IGNOU Special Institute
}
substring(char *k)  Free Trial Classes
{
ln=strlen(k);  Subjective Knowledge
}
substring operator-(substring obj1)  Free PIXELES Guide Books (Prepared by
{
substring temp; our teachers)
temp.ln=ln-obj1.ln;
return temp;  Free Solved Assignments
}
void display()  Experienced Faculties
{
 100% Results
cout<<"The length differences is = "<<ln;
}
 Home Test Series
};
 Class Test Series
void main()
{  We teach you until you pass
clrscr();
substring s1("pixeles");  Final Year Synopsis & Project
substring s2("india");
substring s3("");  Proper Guidance
s3=s1-s2;
s3.display();
getch();
}

www.pixelesindia.com
PIXELES CLASSES BCA & MCA (IGNOU)

Question 4.
(a) What is stream manipulator?Explain use of setw()and Setprecision()as stream
manipulator.
Ans:- PIXELES
Stream manipulator is used to manipulate the output formats. <Iomanip> header file is used for CLASSES
manipulators. It define number of function in c++. Page | 10
Setw():-
This function is used to set the number of character to be used as the field width for the next
insertion operation. It is declared in <iomanip.h >header file.
EXAMPLE:-
#include<iostream.h>
#include<conio.h>
void main()
{
cout<<”name=”<<setw(20)<<”pixeles”;
}
Setprecision:-
This function is used to control the number of digit of an output stream to be displayed on the
screen in floating point values. It is declared in iomanip header file.
EXAMPLE:-
#include<iostream.h>
#include<conio.h>
void main()
{
float a,b,c;
a=11;
b=7;
c=a/b;
cout<<setprecision(1)<<c<<endl;
cout<<setprecision(2)<<c<<endl;
}
(b):-Explain the following function for manipulating file pointers, with the help of example
program.
 Seekg()
 Seelp()
Seekg():- This function moves the input(get) pointer to a specified location
Seekg(offset,refposition);
EXAMPLE:-
#include<iostream.h>
#include<conio.h>
#include<string .h>
void main()

www.pixelesindia.com
PIXELES CLASSES BCA & MCA (IGNOU)
{
char name[40];
int i,ln;
cin>>name;
ln=strlen(name);
fstream file; PIXELES
file.open(“text”,ios::in i ios:out) CLASSES
for(i=o;i<ln;i++) Page | 11
{
file.put(name[i]);
file.seekg(0);
char c;
}
while(file)
{
file.get(ch);
cout<<ch;
}
}
 Seekp():-
This function is used moves to output (put) pointer to a special location.
EXAMPLE:-
#include<iostream.h>
#include<conio.h>
#include<string .h>
void main()
{
char name[40];
int i,ln;
cin>>name;
ln=strlen(name);
fstream file;
file.open(“text”,ios::in i ios:out)
for(i=o;i<ln;i++)
{
file.put(name[i]);
file.seekp(0);
char c;
}
while(file)
{
file.get(ch);
cout<<ch;
}

www.pixelesindia.com
PIXELES CLASSES BCA & MCA (IGNOU)
}

(c) What is exception? Write a program in c++ to perform simple arithmetic operations
with proper exceptions handling.
Ans:-
Exception is an abnormal condition that occurs during execution of program. It is not an PIXELES
error (syntax or logical)but it does not pass the execution of program in some special case. CLASSES
There are three keyword used in exception handling:- Page | 12
 Try
 Catch
 Throw
example:-
#include<conio.h>
#include<iostream.h>
void main
{
int a,b,c;
cout<<”enter the value of a and b”;
cin>>a>>b;
try
{
if(b!=0)
{
c=a/b;
}
else
{
throw(b);
}
cout<<c;
}
catch(int x)
{
cout<<”b can not be zero”;
}
getch();
}
Question5.
(a) What is template? Write appropriate statements to create a template class for
queue data structure in c++
Ans:-
#Include<iostream.h>
#include<conio.h>
template<class t>

www.pixelesindia.com
PIXELES CLASSES BCA & MCA (IGNOU)
class queue
{
t arr[max];
t front,rear;
void add();
void delete(); PIXELES
queue() CLASSES
{ Page | 13
rear=front=-1
}
};
void queue::add(t n);
{
if(rear=max-1)
{
cout<<”overflow”;
}
else if(front=rear=-1)
{
front=0;
rear=0;
arr[max]=n;
}
else
{
rear++;
arr[rear]=n;
}
}
void queue: delete()
{
t x;
if(front==-1)
{
cout<<”underflow”;
}
else
{
x=arr[front];
arr[front]=0;
front++;
cout<<”deleted element is”<<x;
}
}

www.pixelesindia.com
PIXELES CLASSES BCA & MCA (IGNOU)
void main()
{
queue<int> q;
q.add(20);
q.delete();
getch(); PIXELES
} CLASSES
Page | 14
(b) What is function overloading? How it is different from function overriding? Explain
with an example.
FUNCTION OVERLOADING
Ans:- More than one function having same name but different argument signature, is called
function overloading.
Example:-
#include<iostream.h>
#include<conio.h>
class pixeles
{
int s;
public:
void sum(int x.int y)
{
s=x+y;
cout<<”result=”<<s;
}
void sum(int x,int y,int z)
{
s=x+y+z;
cout<<”result is=”<<s;
}
};

void main()
{
pixeles ob;
ob.sum (4, 3);
ob.sum (7, 3, 2);
getch ();
}
Function overriding:-A same function is defined in base class as well as in derived class is
called function overriding.
Example:-
#include<iostream.h>
#include<conio.h>

www.pixelesindia.com
PIXELES CLASSES BCA & MCA (IGNOU)
class employee
{
int eno;
char n[10];
public:
void input() PIXELES
{ CLASSES
cout<<”ente the e no”; Page | 15
cin>>eno;
cout<<”enter the name”;
cin>>n;
}
void display()
{
cout<<”eno is”<<eno;
cout<<”name is”<<n;
}
};
class manager: employee
{
int sal;
public:
void input()
{
employee::input();
cout<<”enter the salary”;
cin>>sal;
}
void display()
{
employee::display();
cout<<”salary is”<<sal;
}
};
void main()
{
manager m;
m.input();
m.display();
Disclaimer: This Assignment is prepared by Our Students.
getch(); Institution and publisher are neither responsible for the result of the
} any action taken on the basis of this work or any omissions or errors.

www.pixelesindia.com

You might also like