You are on page 1of 26

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.

com)

11

Concept of Object
Chapter-1
Oriented
Programming

History & features: Its need & requirement, procedure oriented programming
versus object oriented programming, basic concepts object oriented programming,
object oriented languages.
Beginning with C++: Concepts & structure of C++ programming, concepts of
structure.
Q1.
Write short note on software evolution
Ans.C++ is an object-oriented programming language. It was developed by Bjarne
Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey, USA, in the early
1980s. Stroustrup, an admirer of Simula67 and a strong supporter of C, wanted to
combine the best of both the language and create a more powerful language that could
support object-oriented programming features and still retain the power and elegance of
C. The result was C++.
Therefore, C++ is an extension of C with a major addition of the class construct
feature of simula67. Since the class was a major addition to the original C language,
Stroustrup initially called the new language C with classes. However, later in 1983, the
name was changed to C++.
The idea of C++ comes from the C increment operator ++, thereby suggesting that
C++ is an augmented (incremented) version of C.
During the early 1990s the language underwent a number of improvements and
changes. In November 1997, the ANSI/ISO standards committee standardized these
changes and added several new features to the language specifications.
C++ is a superset of C. Most of what we already know about C applies to C++ also.
Therefore, most all C programs are also C++ programs. However, there are a few minor
differences that will prevent a C program to run under compiler.
The most important facilities that C++ adds on to C are classes, inheritance,
function overloading, and operator overloading. These features enable creating of
abstract data types, inherit properties from existing data types and support
polymorphism, thereby making C++ a truly object-oriented language.
The object-oriented features in C++ allow programmers to build large programs
with clarity, extensibility and ease of maintenance, incorporating the spirit and efficiency
of C. The addition of new features has transformed C from a language that currently
facilitates top-down, structured design, to one that provides bottom-up, object-oriented
design.
Q2.Define OOP(object oriented programming)
Ans. :Object Oriented programming is an approach that provides a way of modularizing
programs by creating partitioned memory area for both data and functions that can be
used as templates for creating copies of such modules on demand. Thus an object is
1

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


considered to be a partitioned area of computer memory that stores data and set of
operations that can access that data.

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


Q3.Explain what is the meaning of OOP and POP
Ans. Its approach is viewed as sequence of things to be done like reading, calculating
and printing. Number of functions is written to accomplish these tasks. The primary
focus is on function procedure of structured oriented programming is as shown.

main()
main()
Function1
Function1

Function2
Function2
Function3
Function3

While we concentrate on development of function, very little attention is given to the data
that are being used by various functions. Data is after all the reason for program
existence. For e.g. the important part of the program is not the function that displays the
data, or a function that checks the correct input. But yet the data is given secondary
status in procedure oriented.
Characteristics of POP (Procedure Oriented Programming)

1. Emphasis is on doing things such as reading, calculating and printing.


2. Large programs are divided into smaller one called as functions.
3. Most of the functions share the global data.
4. Data moves openly around the system.
5. Implies top-down approach.
6. New data type cant be handled easily as built in data types.
Example : If we want to work with complex number which are user define data type
they cant behave similar to built in data type. That is one complex number cant be
added to another using + operator
Object Oriented Programming
The major motivation factor in the invention of OOPS is to solve the pitfalls
encountered in POP. The organization of data and function in OOPS is shown below.
Object1
Data
Function( )
Object 2
Data
Function ( )
Object 3
3

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


Data
Function ( )
The fundamental idea behind oops is to combine into single unit both data & functions
such unit is called as object.
The object members are referred as member functions in C++.
Example. Think of the object as department in an organization such as sales, finance &
personal. These departments provide an approach to cooperate the organization.
Features of OOPS
1. Emphasis is on data rather than procedures.
2. Programs are divided into objects.
3. Functions that operate on data of objects are tied together in data structure.
4. Data is hidden and cannot be accessed by the external functions.
5. Objects may communicate through functions.
6. New data functions can be easily added.
7. It is a bottom- up approach.
Q4.List the drawbacks of Procedure oriented Languages
Ans.Disadvantages of Procedure oriented Language are
1. No security for data. If we declare a variable before main function then it can
be accessed freely from any function present in the program.
2. No better memory management.
3. Difficult to implement today's client requirements.
4. No structure or code reusability. Hence time of development, testing and length
of program increases.
5. As length of application increases it causes slow performance.
6. Code maintenance and enhancements are difficult.
7. No proper way method for Exception handling.
Q5.What are the characteristics of Procedure oriented languages
Ans.Characteristics of POP (Procedure Oriented Programming) are

1. Emphasis is on doing things such as reading, calculating and printing.


2. Large programs are divided into smaller one called as functions.
3. Most of the functions share the global data.
4. Data moves openly around the system.
5. Implies top-down approach.
6. New data type cant be handled easily as built in data types.
Example. If we want to work with complex number which are user define data type
they cant behave similar to built in data type. That is one complex number cant be
added to another using + operator
Q5.With the help of neat sketch show the relation of data and function in POP
languages
Ans. Procedure-oriented programming basically consists of writing a list of instructions
(or actions) for the computer to follow and organizing these instructions into groups
known as functions. We normally use a flowchart to organize these actions and represent
4

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


the flow of control from one action to another. While we concentrate on the development
of functions, very little attention is given to the data that are being used by various
functions. What happens to the data? How are they affected by the functions that work
on them?
When there are more then one program, many important data items are placed as global
so that they may be accessed by all the functions. Each function may have its own local
data. Figure shows the relationship of data and functions in a procedure-oriented
program.

Global data are more vulnerable(Weak) to an inadvertent change by a function. In a large


program it is very difficult to identify what data is used by which function. In case we
need to revise an external data structure, we also need to revise all functions that access
the data. This provides an opportunity for bugs to creep in. Another serious drawback
with the procedural approach is that it does not mode real world problems very well. This
is because functions are action-oriented and do not really correspond to the elements of
the problem.
Q7.What are the features of object oriented language
Ans. Features of OOPS are
1. Emphasis is on data rather than procedures.
2. Programs are divided into objects.
3. Functions that operate on data of objects are tied together in data structure.
4. Data is hidden and cannot be accessed by the external functions.
5. Objects may communicate through functions.
6. New data functions can be easily added.
7. It is a bottom- up approach.
Q8. List and Describe basic concepts of OOP
Ans. Basic Concepts of OOPS are as follows :
1. Objects
2. Classes
3. Data abstractions
4. Data Encapsulation
5. Inheritance
5

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


6. Polymorphism
7. Dynamic binding
8. Message passing
Objects
Objects are basic run time entitles in an object oriented system. They may
represent a person, a place, a bank account that a program must handle. They may also
be a user defined functions such As vectors, date and time. Program object should be
chosen such that they match closely the real World object.
Example. Elements of computer user environment are windows, menus and graphics,
objects.The physical objects are automobile in graphics stimulation, electronic
component in circuit designing. The match between programming objects and a real
world objects is a good resulting Objects offering the revolution in programming.
For example: students objects includes the data members and functions as given:
Student

Object : Student
Data :
Name
Data of Birth
Marks
Functions
Total
Average
Display

Total
Average

Display

Classes
In OOPS, The entire set of data & codes of an object can be made a user define
data types. With the help of classes. A class is collection of objects of similar type. The
objects are the members of classes almost all computer languages have built in data
types like integer, float etc. In similar way you can define many objects of same class
and the entire code of an object can be made a user define data type with the help of a
class. A class is thus a collection of objects of similar type. For e.g staff and students are
the members of the college.
Data Abstraction and Encapsulation
The wrapping up of data and function into a single unit is called as
Encapsulation. The data is not accessible to the outside world and only those
functions, which are members of class, can access the data. These functions provide
interface between objects and programs. This insulation of data from direct access by
program is called as data hiding. Abstraction refers to the representation of essential
features without including background details or explanations. Classes use the concept
of abstraction and are defined as a list of abstract attributes such as size, cost, etc.
Functions to operate on these attributes.
Inheritance
6

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


It is a process by which the object of one class acquires the properties of objects of
another class. It supports the concept of hierarchical classification. For e.g. sparrow is a
part of class flying birds, which again is a part of class bird as, shown in fig. The
principle behind this is that each derived class shares the common characteristics with
the class from which it is derived.
In OOPS, the concept of inheritance provides the idea of reusability if we can add
additional features of an existing class without modifying it. This is possible by deriving a
new class from existing one. The new class will have the combined features of both the
classes.

BIRD
BIRD
FLYING
FLYINGBIRD
BIRD

NON-FLYING
NON-FLYINGBIRD
BIRD

SPARROW
SPARROW
Polymorphism
Polymorphism means the ability to take more than one form. For e.g. an operation
may exhibit different behavior in different instants. The behavior depends upon the types
of data used in the operation.
For e.g. consider operation addition for two numbers, the operation will generate sum. If
operands are strings the operation would perform a third string by concatenation. To
handle function overloading, a single function name can be used to handle different
numbers and different types of arguments. This is something similar to particular word
having several different meanings depending upon the context as shown in the fig.
SHAPE
SHAPE
CIRCLE
CIRCLE
DRAW(
DRAW() )

RECTANGLE
RECTANGLE
DRAW(
DRAW() )

POLYGON
POLYGON
DRAW(
DRAW() )

Dynamic binding
Binding refers to a linking procedure call to the code, which is to be executed in
response to the call. Dynamic binding means that the code associated with a given
procedure call is not known until the time of call at the run time.
Consider a procedure draw shown in a fig by inheritance every object will have
this procedure. This draw procedure will be redefined in each class that defines the
object. At the run time, the code matching the object under current reference will be
called.
7

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


Message communication
An object-oriented program consists of set of objects that communicate with each
other. The process of programming in an object oriented language involves following
basic steps: 1. Creating classes that define object and their behavior.
2. Creating object from class definition.
3. Establishing communication among object.
Objects may communicate with one another by sending and receiving information
through functions. A message for an object is a request for execution of a procedure.
Therefore it evolves a function in receiving object that generates desired results.
For example
Employee.salary(name);
Employee object
Salarymessage
Information information
Q9.List the benefits of OOPS
Ans. OOP offers several benefits to both the program designer and the user. Objectorientation contributes to the solution of many problems associated with the
development and quality of software products. The new technology promises greater
programmer productivity, better quality of software and lesser maintenance cost. The
principal advantages are:
1. Through inheritance, we can eliminate redundant code and extend the use of
existing classes.
2. We can build programs from the standard working modules that communicate
with one another, rather than having to start writing the code from scratch. This
leads to saving of development time and higher productivity.
3. The principle of data hiding helps the programmer to build secure programs that
cannot be invaded by code in other parts of the program.
4. It is possible to have multiple instances of an object to co-exist without any
interference.
5. It is possible to map objects in the problem domain to those in the program.
6. It is easy to partition the work in a project based on objects.
7. The data-centered design approach Enables us to capture more details of a model
in implement able form
8. Object-oriented systems can be easily upgraded from small to large systems.
9. Message passing techniques for communication between objects makes the
interface descriptions with external systems much simpler.
10. Software complexity can be easily managed.
Q10.State the difference between OOP and POP languages
Ans.
POP (Procedure Oriented Language)
OOP (Object Oriented Language)
1.Data can be accessed by external
Data cant be accessed by external
functions.
functions.
2.It employees Top-Down Approach.
It employees Bottom-Up approach.
8

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


3.Programs are divided into functions.
4.New data types can be easily
handled.
5.Most of the function share global
data.

Programs are divided into objects.


New data types can be easily handled.
Data structures are designed such that
they characterize the object.

Q11.Which Language are called as Object Based Programming Languages


Ans. Object Based Programming is the style of programming that support encapsulation
& object identity. Major features of it are
i. Data encapsulation
ii. Data hiding
iii. Automatic installation
iv. Operator overloading
They dont support inheritance & dynamic binding. The example for object-based
language is the language name is ADA.
Q12.Which languages area called as Object oriented Languages
Ans. In corporate all object base programming features along with two additional
features that is inheritance & dynamic binding. So
Object oriented= Object based+ inheritance + Dynamic binding.
The language that supports these features is C++, Small talk, Object Pascal & java.
Usually the java is purely object-oriented language.

Object Oriented Language


Like the structure programming we can implement the concept of oops using the
language such as C & PASCAL. But that becomes the confusion where the program
grows larger. Hence a language that is specially designed to support OOPS language
The language should support many concept of OOPS to claim that they are OOPS
depending on the feature they support. They are classified in following two categories.
1) Object Based Programming Language
2) Object Oriented Programming Language
1) Object Based Programming is the style of programming that support encapsulation
& object identity. Major features of it are
1. Data encapsulation
2. Data hiding
3. Automatic installation
4. Operator overloading
They dont support inheritance & dynamic binding. The example for object-based
language is the language name is ADA.
2)Object Oriented Programming Language corporate all object base programming
features along with two additional features that is inheritance & dynamic binding. So
Object oriented= Object based+ inheritance + Dynamic binding.
9

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


The language that supports these features is C++, Small talk, Object Pascal & java.
Usually the java is purely object-oriented language.
Q13.List characteristics of some OOP languages
Ans.

Q14. List some Object Oriented Language and Object based Programming
Languages
Ans. Object oriented Languages
C++
Small talk
Object Pascal
Java
Object Based Programming languages
ADA
Visual Basic
Cardelli 85
Modula-3
10

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


Q15.State application of object oriented languages
Ans.Applications of OOPS
The concepts of OOPS are gaining importance in many areas. The most popular
application has being in the area of
1. Real-time systems
2. Simulation and modeling
3. Object-oriented databases
4. Hypertext, hypermedia and expertext
5. AI and expert systems
6. Neural networks and parallel programming
7. Decision support and office automation systems
8. CIM/CAM/CAD systems
Q16.Describe how data and Functions are organized in OOP
Ans. The major motivating factor in the invention of object-oriented approach is to
remove some of the flaws encountered in the procedural approach. OOP treats data as a
critical element in the program development and does not allow it to flow freely around
the system. It ties data more closely to the functions that operate on it, and protects it
from accidental modification from outside functions.

OOP allows decomposition of a problem into a number of entities called objects


and then builds data and functions around these objects. The organization of data and
functions in object-oriented programs is shown in Fig. The data and object can be
accessed only by the functions associated with that object. However, functions of one
object can access the functions of other objects.
Q17.Why to use C++ as object oriented language
Ans.
1. Implementation details not related to the problem are isolated in the low-level
parts of the other pieces of the program, e.g..., If we try to write a C program to
read in two strings, concatenate them, and print the result, we are immediately
11

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


distracted form our goal by the details of string processing. How long the string
be? check to see if the allocation function failed, and remember to free the storage
when no longer need it.
2. If we build large programs with only procedural abstractions, we may have
difficulty organizing and remembering all procedures
3. Since each data abstraction can contain many procedures, data abstraction
provides a way to group our procedures into large units. The most significant
advantage of OOP is the way theprogrammer thinks about the program:
4. A programmer working on operating system might think in terms of processes,
device, and users
5. A programmer working on windows system might think of things that can be
displayed on the windows ( graphical images, text, etc..)
6. OOP allows the programmer to create abstractions in the program that match
these concepts C++ supplies all the language features of C, and adds features to
support OOP. Language feature that support OOP includes:
7. Support for data abstraction for user-defined types
8. Features to let users work with user-defined types in the same ways they work
with built-in types.
9. Inheritance, a mechanism for grouping common features of several data
abstractions
10. Dynamic Binding
11. C programs can be re-coded in C++ with little or no loss of efficiency
12. C++ like C, allows the programmer to bend the rule of data encapsulation
Q18.Describe the structure of C++ program
Ans. It is a common practice to organize a program into three separate files. The class
declarations are placed in a header file and the definitions of member functions go into
another
file
This
approach enables the
programmer
to
separate the abstract
specification of the
interface
(class
definition) from the
implementation details
(member
functions
definition).
Finally, the main program that uses the class is placed in a third file which
includes the previous two files as well as any other files required.
This approach is based on the concept of client-server model as shown in Fig. The
class definition including the member functions constitute the server that provides
services to the main program known as client.
The client uses the server through the public interface of the class.
Q19.State application of C++?
Ans. C++ is a versatile language for handling very large programs. It is suitable for
virtually any programming task including development of editors, compilers, databases,
communication systems and any complex real-life application systems.
12

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


Since C++ allows us to create hierarchy-related objects, we can build special
object- oriented libraries which can be used later by many programmers.
While C++ is able to map the real-world problem properly, the C part of C++ gives
the language the ability to get close to the machine-level details.
C++ programs are easily maintainable and expandable. When a new feature needs
to be implemented, it is very easy to add to the existing structure of an object.
It is expected that C++ will replace C as a general-purpose language in the near
future.
Q20.Describe some programming features of C++ programming language.
Ans. Some Programming Features of C++
Like C, the C++ program is a collection of functions. The program contains only one
function main (). As usual, execution begins at main(). Every C++ program must have a
main (). C++ is a free-form language. With a few exceptions, the compiler ignores carriage
returns and white spaces. Like C, the C++ statements terminate with semicolons.
Comment
C++ introduces a new comment symbol (double slash). Comments start with a double
slash symbol and terminate at the end of the line. A comment may start anywhere in the
line, and whatever follows till the end of the line is ignored. Note that there is no closing
symbol.
// This is Example of C++
We can use either or both styles in our programs.
/*
This is also example of
oops using c++
*/
Output Operator
cout << C++ is better than C.;
This statement causes the string in quotation marks to be displayed on the screen. This
statement introduces two new C++ features,
cout and <<. The identifier cout (pronounced as
C out) is a predefined object that represents the
standard output stream in C++. Here, the
standard output stream represents the screen.
It is also possible to redirect the output to other
output devices.
The operator << is called as insertion or
put operator
The operator << is the bit-wise left-shift
operator and it can still be used for this
purpose. This is an example of how one operator
can be used for different purposes, depending
on the context. This concept is known as operator overloading, an important aspect of
polymorphism.
The IOstream File
13

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


This directive causes the preprocessor to add the contents of the iostream file to the
program. It contains declarations for the identifier cout and the operator <<. Some old
versions of C++ use a header file called iostream.h. This is one of the changes
introduced by ANSI C++.
The header file iostream should be included at the beginning of all programs that
use input/output statements.
Namespace
Namespace is a new concept introduced by the ANSI C++ standards committee.
This defines a scope for the identifiers that are used in a program. For using the
identifiers defined in the namespace scope we must include the using directive, like
using namespace std;
Here, std is the namespace where ANSI C++ standard class libraries are defined. All ANSI
C++ programs must include this directive. This will bring all the identifiers defined in ski
to the current global scope. using and namespace are the new keywords of C++.
Input Operator
The statement
Cin>>number1;
is an input statement and causes the program to wait for the user to type in a number.
The number keyed in is placed in the variable number1. The identifier cin (pronounced
C in) is a predefined object in C++ that corresponds to the standard input stream. Here,
this stream represents the keyboard.
The operator >> is known as extraction or get from operator. It extracts (or takes) the
value from the keyboard and assigns it to the variable on its right . This corresponds to
the familiar scanf() operation. Like << , the operator >> can also be overloaded.
Cascading the Operator
We have used the insertion operator <<repeatedly in the last two statements for printing
results.The statement
cout << Sum = << sum <<\n
first sends the string Sum = to cout and then sends the value of sum. Finally, it sends
the newline character so that the next output will be in the new line. The multiple use of
<< in one statement is called cascading. When cascading an output operator, we should
ensure necessary blank spaces between different items.
Similarly input operator can also be cascaded
Cin>>number1>>number2
Q21.Describe structure in C++?
Ans. The C++ class is an extension of the C language structure. Because the only
difference between a structure and a class is that structure members have public access
by default and class members have private access by default, you can use the keywords
class or struct to define equivalent classes.
For example, in the following code fragment, the class X is equivalent to the structure Y:
class X
{
// private by default
14

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


int a;
public:
int f()
{

// public member function


return a = 5;

};
};
struct Y
{
// public by default
int f()
{
return a = 5;
};
private:
int a;

// private data member

};
Q22.Compare Structure and Class
Ans. In C++, a structure is a class defined with the struct keyword. Its members and
base classes are public by default. A class defined with the class keyword has private
members and base classes by default. This is the only difference between structs and
classes in C++.
Basically in C++ structure and class are same but with some minor differences listed
below
1. In classes all the members by default are private but in structure members are
public by default.
2. There is no term like constructor and destructor for structs, but for class compiler
creates default if you don't provide.
3. Sizeof empty structure is 0 Bytes were as Sizeof empty class is 1 Byte
4. Inheritance between classes is also private by default, and inheritance between
structs is public by default.
5. Structures didnot support data hiding, but classes can.
6. Private functions totally hidden in classes.
7. classes support polymorphism, whereas structures donot
Q.23.Compare C and C++ Structure
Ans.
The definition of the structure in C is limited to within the module and cannot
be initialized outside its scope. Where as in C++ you can initialize the objects
anywhere within the boundaries of the project.
The one more difference is in C++ structure we can have methods(procedures)
but C can note have methods in structure
15

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)

By default C structure are Public while C++ structure are private. In C you can
declare structure as struct A a; But not A a(which is possible in C++);

Q.Compare POP and OOP


Ans.

The object oriented program deals with objects created on the basis of classes
example: java c++, but procedure oriented program deals with functions example:c
, pascal
In Procedural programming Programmer combines related sequences of
statements into one single place called procedure. A procedure call is used to
invoke the procedure. After the sequence is processed flow of control proceeds
right after the position where the call was made. But the approach in oops is that
classes and objects are used to model real world entity taking help of methods
which performs the functions.
Procedure oriented programming - Gives importance to algorithm rather than
data. It follows Top-down structures. Object oriented programming - Gives
importance to data rather than algorithm. It follows Bottom-up structures.Every
thing is viewed as an object.It includes features like data hiding polymorphism
encapsulation and message passing.
OOP supports classes objects and also
1. Incapsulation - Wrap the data in single unit
2. Inheritance - Aquire the properities of parent class
3. Polymorphism - Same function can be used with different signatures

Program based on C++


Example 1 :Program based array of structure
Ans.
#include<iostream.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[20];
int m;
} s[5];
void main()
{
int i,j,temp;
char temp1[20];
for(i=0;i<5;i++)
{
16

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


cout<<"\n Enter the name of student "<<i;
cin>>s[i].name;
cout<<"\n Enter the marks of student "<<(i+1);
cin>>s[i].m;
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(s[i].m>s[j].m)
{
temp=s[j].m;
s[j].m=s[i].m;
s[i].m=temp;
strcpy(temp1,s[j].name);
strcpy(s[j].name,s[i].name);
strcpy(s[i].name,temp1);
}
}
for(i=0;i<5;i++)
{
cout<<"\n Name : "<<s[i].name<<"\t Marks : "<<s[i].m;
}
getch();
}

Example 2 : Program for Searching for an address


Ans.
#include <iostream.h>
int* location(int[],int,int);
void main()
{ int a[8] = {22, 33, 44, 55, 66, 77, 88, 99}, * p=0, n;
do
{ cin >> n;
if (p == location(a, 8, n)) cout << p << ", " << *p << endl;
else cout << n << " was not found.\n";
} while (n > 0);
}
int* location(int a[], int n, int target)
{ for (int i = 0; i < n; i++)
if (a[i] == target) return &a[i];
return NULL;
}
Example 3 :Program Swap Two numbers using pointers and Without Pointers
#include<iostream.h>
17

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


#include<string.h>
#include<conio.h>
void swap (char p, char q)
{
char c ;
c=p;
p=q;
q=c;
}
void swap1 (char *p, char *q)
{
char c ;
c = *p ;
*p = *q ;
*q = c ;
}
void perm (char *a, int m, int n)
{
if (m == n)
{
for (int i = 0 ; i < n ; i ++)
cout<<a [ i ] ;
}
else
{
for (int j = n ; j<=m;j++)
{
swap(a[j],a[n]) ;
perm(a,m,n+1);
swap1(&a[j],&a[n]);
}
}
}
void main( )
{
char *a = "India";
perm(a, strlen(a)-1, 0);
getch( );
}
Example 4 : Program to Demonstrate Function returning a Pointer
Ans.
#include <iostream.h>
#include <conio.h>
void main ()
{
18

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


int *square ( int *a ) ;
int i, *result ;
clrscr () ;
cout<<"i = " ;
cin>>i;
result = square ( &i ) ;
cout<<"square ( i ) : "<<*result ;
}
int *square ( int *a )
{
*a = *a * *a ;
return ( a );
}
Example 5 : Program to demonstrate Pointers and Array
#include <iostream.h>
#include <conio.h>
void main ()
{
int array[5] = { 11, -2, 30, 41, 15 } ;
int *pointer ;
clrscr () ;
pointer = &array[0] ;
cout<<"The array elements are.....";
while ( pointer < pointer + 5 )
{
cout<<*pointer<<"\n";
pointer++ ;
}
getch () ;
}
Example 6 : Program for Array name as a pointer - second example
#include <iostream.h>
#include <conio.h>
void main ()
{
void multiply_by_2 ( int a[] ) ;
int array[5] = { 11, -2, 30, 41, 15 } ;
int *start, *end ;
clrscr () ;
multiply_by_2 ( array ) ;/* name of the array is passed to function */
start = array ;
end = array + 5 ;
19

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


cout<<"The array elements are....." ;
for ( ; start < end ; start++ )
cout<<*start<<endl;
getch () ;
}
void multiply_by_2 ( int a[] )
{
int i ;
for ( i = 0; i < 5; i++ )
a[i] = a[i] * 2 ;
}
Example 7 : Program for Structure passed to a function ( by Value )
#include <iostream.h>
#include <conio.h>
struct student
{
int student_roll_no ;
int marks_in_subject1 ;
int marks_in_subject2 ;
};
void main ( )
{
void print_structure ( struct student s1 ) ;
float find_average ( struct student s1 ) ;
struct student s1 = { 3032, 89, 78 } ;
clrscr ( );
/* Structure variable passed by value */
print_structure ( s1 ) ;
cout<<"Average Marks = "<<find_average ( s1 ) ;
getch ( ) ;
}
void print_structure ( struct student s1 )
{
cout<<"Student Record : \n";
cout<<"Roll Number
: "<<s1.student_roll_no<<endl;
cout<<"Subject 1 marks : "<<s1.marks_in_subject1<<endl;
cout<<"Subject 2 marks : "<<s1.marks_in_subject2<<endl;
}
float find_average ( struct student s1 )
{
float average ;
average = s1.marks_in_subject1 + s1.marks_in_subject2 ;
average = average / 2 ;
return ( average ) ;
}
20

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


Example 8 : program to demonstrate function returning a Structure
Ans.
#include <iostream.h>
#include <conio.h>
struct student
{
int student_roll_no ;
int marks_in_subject1 ;
int marks_in_subject2 ;
};
void main ()
{
struct student function () ;
struct student t ;
float result ;
clrscr () ;
t = function () ;
cout<<"Student Record : \n";
cout<<"Roll Number
: "<<t.student_roll_no;
cout<<"Subject 1 marks : "<< t.marks_in_subject1 ;
cout<<"Subject 2 marks : "<< t.marks_in_subject2 ;
getch () ;
}
struct student function ( )
{
struct student s1 = { 3032, 89, 78 } ;
return ( s1 ) ;
}
Example 9 : Program to demonstrate Array as a member of a Structure
Ans.
#include <iostream.h>
#include <conio.h>
struct employee
{
char name[10] ;
char address[15] ;
long int telephone_numbers[5] ;
};
void main ( )
{
struct employee input ( ) ;
struct employee e ;
int i ;
clrscr ( ) ;
e = input ( ) ;
cout<<"\n\nEmployee Personal Information displayed... \n";
cout<<"\nName : "<<e.name ;
21

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


cout<<"\nAddress : "<<e.address ;
cout<<"\nTelephone Numbers : ";
for ( i = 0; e.telephone_numbers[i] > 0 && i < 5; i++ )
cout<<"\n"<<e.telephone_numbers[i] ;
getch ( ) ;
}
struct employee input ( )
{
struct employee temp ;
long int no;
int i ;
cout<<"Input the following -\n";
cout<<"Name : ";
cin>>temp.name ;
cout<<"Address : ";
cin>>temp.address;
cout<<"Telephone Numbers :\n";
cout<<"(terminate with -ve number,\n" ;
cout<<"if want to enter less than 5 telephone numbers)\n";
i = 0;
cin>>no ;
/*
User can enter either 5 differrent telephone numbers or less.
If he enters less than 5 telephone numbers then he may terminate by entering
negative number */
while ( no > 0 && i < 4 )
{
temp.telephone_numbers[i] = no ;
i++ ;
//scanf("%ld",&no) ;
cin>>no;
}
temp.telephone_numbers[i] = no ;
return ( temp ) ;
}
Example 10 : Program to demonstrate Array of Structures
Ans.
#include <iostream.h>
#include <conio.h>
struct employee
{
char name[10] ;
char address[15] ;
long int telephone_numbers[5] ;
22

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


};
void main ( )
{
struct employee input ( ) ;
struct employee e[5] ;
int i, j, n ;
clrscr ( ) ;
cout<<"Enter Number of Employees ( less than 5 ) : ";
cin>>n ;
for ( i = 0; i < n; i++ )
e[i] = input ( ) ;
clrscr ( ) ;
cout<<"\n\nEmployee Personal Information displayed... \n";
for ( i = 0; i < n; i++ )
{
cout<<"\nName : "<<e[i].name<<endl;
cout<<"Address : "<<e[i].address<<endl;
cout<<"\nTelephone Numbers :\n";
for ( j = 0; e[i].telephone_numbers[j] > 0 && j < 5; j++ )
cout<<e[i].telephone_numbers[j];
}
getch ( ) ;
}
struct employee input ( )
{
struct employee temp ;
long int no;
int i ;
cout<<"Input the following -\n" ;
cout<<"Name : " ;
cin>>temp.name ;
cout<<"Address : ";
cin>>temp.address ;
cout<<"Telephone Numbers : ";
i = 0;
cin>>no;
while ( no > 0 && i < 4 )
{
temp.telephone_numbers[i] = no ;
i++ ;
cin>>no;
}
temp.telephone_numbers[i] = no ;
return ( temp ) ;
}
23

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


Example 11 : Array of Students as a function argument
#include <iostream.h>
#include <conio.h>
struct student
{
int roll_no ;
int physics_marks ;
int chemistry_marks ;
float average_marks ;
};
void main ()
{
void find_average ( struct student a[] ) ;
/* array of structure initialisation -( average_marks are initially set to 0 ) */
struct student s[5] = { { 1, 50, 70, 0 },
{ 2, 67, 89, 0 },
{ 3, 78, 87, 0 },
{ 4, 56, 77, 0 },
{ 5, 49, 45, 0 } } ;
int i ;
clrscr ();
find_average ( s ) ;
cout<<"Student Information --\n\n" ;
for ( i = 0; i < 5; i++ )
{
cout<< "Student Roll No. : "<<s[i].roll_no<<endl ;
cout<< "Physics marks : "<<s[i].physics_marks<<endl;
cout<<"Chemistry marks : "<<s[i].chemistry_marks<<endl;
cout<<"Average marks : "<<s[i].average_marks<<endl;
}
getch ();
}
void find_average ( struct student a[] )
{
int i ;
for ( i = 0; i < 5; i++ )
{
a[i].average_marks = a[i].physics_marks + a[i].chemistry_marks ;
a[i].average_marks /= 2 ;
}
}
Board Exam Questions
Winter 2007
24

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


a. list any four object oriented programming languages.
Ans.Refer Q.No.
b. Give syntax and example of defining structure and declaring structure variable
Ans.Refer Q.No.
c. Give significance of & operator and * operator.
Ans.Refer Q.No.
Summer 2008
a. Give Four characteristics of object oriented programming
Ans.Refer Q.No.
b. Which are input and output operator in C++. Give its example
Ans.Refer Q.No.
c. Compare strcuture and class
Ans.Refer Q.No.
Winter 2009
a. Mention the concepts of OOPS
Ans.Refer Q.No.
b. Distinguish between OOP and POProg.
Ans.Refer Q.No.
Summer 2009
a. List any four features of procedure oriented language.
Ans.Refer Q.No.
b. List any four basic concepts of object oriented programming.
Ans.Refer Q.No.
c. Distinguish between procedure oriented and object oriented programming
Ans.Refer Q.No.
Winter 2009
a. What is meant by object oriented programming language? Enlist any two object
oriented programming languages.
Ans.Refer Q.No.
b. List application of oops technology.
Ans.Refer Q.No.
Summer 2010
a. Define structure. Give syntax of defining structure.
Ans.Refer Q.No.
25

Prof.Manoj.Kavedia (9860174297) (urallalone@yahoo.com)


b.Explain an four features of object-oriented programming.
Ans.Refer Q.No.

26