Data Structures and Object Oriented Programming in C++
Data Structures and Object Oriented Programming in C++
Introduction
The main pitfall with standard C has been identified as the lack of
facilities for data abstraction. With the emergence of more abstract and
modular languages like Modula-2 and Ada and Object-oriented languages
like Small-Talk, BJARNE STROUSRUP at Bells Lab was motivated to
develop C++ by upgrading C with appropriate mechanisms to create object
like abstract data structures. The introduction of the class mechanism in C++
mainly provides the base for implementing abstract data types to suit o bject-
oriented programming. The C++ language is thus considered as the superset
of the standard C.
Identifiers can be defined as the name of the variables and some other
program elements using the combination of the following characters.
Eg.,
NAME, A23C, CODE, EMP_NAME
Special characters :
All characters other than listed as alphabets, numerals and underscore, are
special characters.
www.annauniversityplus.com
1
Keywords
Keywords are also identifiers but cannot be user defined since they are
reserved words.
Keywords in C++
Auto break case char const continue default do double else enum
extern float for goto if long register return short signed sizeof
static struct switch union unsigned void volatile while
Constants
String constants
Numeric constants
Character constants
String constants
www.annauniversityplus.com
2
Numeric Constants
These are positive or negative numbers.
Types of Numeric constants are :
Integer
Integer
Short Integer(short)
Long Integer(long)
Float
Single precision(float)
Double precision(double)
Long double
Unsigned
Unsigned char
Unsigned integer
Unsigned short integer
Unsigned long integer
Hex
Short hexadecimal
Long Hexadecimal
Octal
Short octal
Long octal
www.annauniversityplus.com
3
Operators
Arithmetic operators (+, -, *, /, %)
Assignment operators (=, +=, -=, *=, /=, %=)
Comparison and Logical operators (<, >, <=, >=, ==,!=, &&, ||, !)
Relational operators (<, >, <=, >=)
Equality operators (==, !=)
Logical operators(&&, ||, !)
Unary operators(*, &, -, !, ++, --, type, sizeof)
Ternary operator (?)
Scope operator(::)
New and delete operators
Program heading
Begin
Type or variable declaration
Statements of operation
Results
End
#include<iostream.h>
void main()
{
cout << “God is Great”;
}
www.annauniversityplus.com
4
iostream
The syntax…
Eg.
Cout << “god is great”
Cout << age
Cout << “name is …” << name
Input Statement
The syntax…..
Cin >> var1 >> var2 >> var3…..varn;
The ‘>>’ is called as “extraction” operator.
Eg.
www.annauniversityplus.com
5
Cout << “Enter a number”
Cin << a
Sample program to add, subtract, multiply and divide of the given two numbers
#include<iostream.h>
void main()
{
int a,b,sum,sub,mul,div;
cout << “Enter any two numbers “ << endln;
cin >> a >> b;
sum=a+b;
sub=a-b;
mul=a*b;
div=a/b;
cout << “Addition of two numbers is…” << sum;
cout << “Mulitiplication of two numbers is…” << mul;
cout << “Subtraction of two numbers is…” << sub;
cout << “Division of two numbers is…” << div;
}
CONTROL STATEMENTS
Conditional Statements
The conditional expressions are mainly used for decision making. The following
statements are used to perform the task of the conditional operations.
if statement
if-else statement
www.annauniversityplus.com
6
switch-case statement
if Statement
If (expression)
{
Statement;
Statement;
;;
;;
}
if-else statement
Syntax
if (expression)
statement;
else
statement;
Syntax
if (expression)
{
block-of-statements;
}
www.annauniversityplus.com
7
else
{
block-of-statements;
}
Nested if
If (expression) {
If (expression) {
**********
**********
}
else {
*********
**********
}
else {
if (expression) {
*******
****
}
else {
*********
*********
}
}
A program to read any two numbers from the keyboard and to display the largest
value of them.
#include<iostream.h>
void main()
{
www.annauniversityplus.com
8
float x,y;
cout << “Enter any two numbers \n”;
cin >> x >> y;
if (x>y)
cout << “Biggest number is…” << x << endl;
else
cout << “Biggest number is…” << y << endl;
}
switch statement
The switch statement is a special multiway decision maker that tests whether an
expression matches one of the number of constant values, and braces accordingly.
Syntax
switch (expression) {
case contant_1 :
Statements;
case contant_2 :
Statements;
;;
;;
;;
case contant_n :
Statements;
default :
Statement;
}
www.annauniversityplus.com
9
A program to find out whether the given character is vowel or not
#include<iostream.h>
void main(){
char ch;
cout << “Enter the character :”
cin >> ch;
switch(ch){
case ‘A’ :
case ‘a’ :
cout << “The given character is vowel “;
break;
case ‘E’ :
case ‘e’ :
cout << “The given character is vowel “;
break;
case ‘I’ :
case ‘i’ :
cout << “The given character is vowel “;
break;
case ‘O’ :
case ‘o’ :
cout << “The given character is vowel “;
break;
case ‘U’ :
case ‘u’ :
cout << “The given character is vowel “;
break;
default :
cout << “The given character is not vowel”;
break;
www.annauniversityplus.com
10
}
Loop Statements
for loop
while loop
do-while loop
for loop
This loop consists of three expressions. The first expression is used to initialize the
index value, the second to check whether or not the loop is to be continued again
and the third to change the index value for further iteration.
Syntax
www.annauniversityplus.com
11
A Program to find the sum and average of given numbers
#include<iostream.h>
void main(){
int n;
cout << “Enter the no. of terms…”;
cin >> n;
float sum = 0;
float a;
for (int i=0; i<=n-1;++i) {
cout << “Enter a number :\n”;
cin >> a;
sum = sum+a;
}
float av;
av = sum/n;
cout << “sum= “ << sum << endl’
cout << “Averave = << av << endl;
}
Nested For-Loops
for(i=0;i<=n; ++i){
for(j=0; j<=m ; ++j)
statements;
statements;
statements;
www.annauniversityplus.com
12
}
while loop
This loop is used when we are not certain that the loop will be executed. After
checking whether the initial condition is true or false and finding it to be true, only
then the while loop will enter into the loop operations.
Syntax
While (Condition)
Statement;
while(condition) {
Statement_1;
Statement_2;
----
----
}
Example
do-while loop
www.annauniversityplus.com
13
Whenever one is certain about a test condition, then the do-while loop can be
used, as it enters into the loop at least once and then checks whether the given
condition is true or false.
Syntax
do{
Statement_1;
Statement_2;
------
------
} while (expression);
Example
Sum=0;
do
{
sum=sum+I;
i++;
}
while (i<n);
break statement
The “break” statement is used to terminate the control from the loop statements of
the “case-switch” structure. This statement is normally used in the switch-case loop
and in each case condition, the break statement must be used. If not, the control
will be transferred to the subsequent case condition also.
Syntax
break;
www.annauniversityplus.com
14
Continue statement
This statement is used to repeat the same operations once again even if it checks
the error.
Syntax
continue;
go to statement
This statement I used to alter the program execution sequence by transferring the
control to some other part of the program.
Syntax
goto label;
There are two types of goto statements, which are conditional and unconditional
goto statement.
Unconditional goto
This goto statement is used to transfer the control from one part of the program to
other part without checking any condition.
Example
#include<iostream.h>
void main(){
start:
cout << “God is Great”;
www.annauniversityplus.com
15
goto start;
}
Conditional goto
This will transfer the control of the execution from one part of the program to the
other in certain cases.
Example
If (a>b)
goto big1;
big1:
Functions
Syntax
Function_type functionname(datatype arg1, datatype arg2,….)
{
body of function;
------
------
return value;
}
return keyword
www.annauniversityplus.com
16
This keyword is used to terminate function and return a value to its
caller. This can be also used to exit a function without returning a value;
Syntax
return;
return(expression);
Types of Functions
The user defined functions may be classified in the following three ways
based on the formal arguments passed and the usage of the “return”
statement, and based on that, there are three types of user-defined
functions.
Type 1
Example
#include<iostream.h>
void main(){
void message() // function declaration
message(); // function calling
}
www.annauniversityplus.com
17
void message(){
----------
---------- // body of the function
----------
}
Type 2
Example
#include<iostream.h>
void main(){
void square(int ) // function declaration
int a;
-------
-------
square(a); // function calling
}
void square(int x){
----------
---------- // body of the function
----------
}
Type 3
Example
#include<iostream.h>
void main(){
int check(int,int,char) // function decl aration
int x,y,temp;
char ch;
----------
----------
temp=check(x,y,ch); // function calling
}
www.annauniversityplus.com
18
int check(int a, int b, char c){
int value;
----------
---------- // body of the function
----------
return(value);
}
A program to find the square of its number using a function declaration without
using the return statement
#include<iostream.h>
Void main()
{
void square(int);
int max;
cout << “Enter a value for n ?\n”;
cin >> max;
for (int i=0;i<=max-1;++i)
square(i);
}
void square(int n)
{
float v;
v=n*n;
cout << “ I = “<< n << “square = “ << value <<endl;
}
A program to find the factorial of a given number using function declaration with the
return statement
#include<iostream.h>
void main(){
long int fact(int);
int x,n;
cout << “Enter the no. to find factorial “ << endl;
cin >> n;
x=fact(n);
cout << “value = “ << n << “and its factorial = “;
cout << x << endl;
}
long int fact(int n)
{
www.annauniversityplus.com
19
int value=1;
if (n == 1)
return (value);
else
{
for (int i=1;i<=n;++i)
value = value * I;
return(value);
}
}
Local variables
Identifiers declared as label, const, type variables and functions in a block are said
to belong to a particular block or function and these identifiers are known as the
local parameters or variables. Local variables are defined inside a function block or
a compound statement.
Eg.
Global variables
Global variables are variables defined outside the main function block.
Eg.
int a,b=10; // global variable declaration
Void main()
{
void func1();
a=20;
::
:;
func1();
}
func1()
www.annauniversityplus.com
20
{
int sum;
sum=a+b;
::
:;
}
The storage class specifier refers to how widely it is known among a set of
functions in a program. In other words, how the memory reference is carried out for
a variable.
Automatic variable
Register Variable
Static variable
External variable
Automatic variable
Internal or local variables are the variables which are declared inside a function.
Eg.
#include <iostream.h>
void main()
{
auto int a,b,c;
::
::
}
Register Variable
These variables are used for fast processing. Whenever some variables are to be
read or repeatedly used, they can be assigned as register variables.
Eg.
function ( register int n)
{
register char temp;
::
www.annauniversityplus.com
21
::
}
Static Variable
Static variables are defined within a function and they have the same scope rules
of the automatic variables but in the case of static variables, the contents of the
variables will be retained throughout the program.
Eg.
Static int x,y;
External variable
Variables which are declared outside the main are called external variables and
these variables will have the same data type throughout the program, both in main
and in the functions.
Eg.
#include<iostream.h>
void main()
{
int funt(registerint x, register int y);
register int x,y,z;
x=0;
y=0;
cout<<”x y”<<endl;
do
{
z=funt(x,y);
cout<<x<<’\t’<<z<<endl;
++x;
++y;
}
while(x<=10);
}
int funt(register int x, register int y);
{
register int temp;
temp=x*y;
www.annauniversityplus.com
22
return(temp);
}
Program to display 1 to 10 with addition of 100 using the automatic variable and
the static variable.
#include<iostream.h>
void main()
{
int func(int x);
int fun(int x1);
int k,s,v;
for (k=1;k<=10;k++)
{
v=func(k);
s=fun(k);
cout<<k<<’\t’<<v<<s<<endl;
}
}
func(int x)
{
int sum=100; // Automatic variable
sum+=x;
return(sum);
}
fun(int x1)
{
static int sum=100; // Static variable
sum+=x1;
return(sum);
}
Recursive Function
A function which calls itself directly or indirectly again and again is known as the
recursive function.
Program to find the factorial of the given no using the recursive function
#include<iostream.h>
void main()
{
long int fact(long int);
int x,n;
cin>>n;
x=fact(n);
www.annauniversityplus.com
23
cout<<n<<x<<endl;
}
Arrays
Syntax
Example
int value[10];
char line[50];
static char page[20];
Array initialization
#include <iostream.h>
void main()
{
int a[5]={44,67,77,88,34,55};
www.annauniversityplus.com
24
int I;
cout << “Contents of the array \n”;
for (i=0;i<5;++i)
{
cout << a[i] << ‘\t’;
}
}
Multidimensional Array
These are defined in the same manner as one dimensional arrays, except
that a separate pair of square brackets are required for each subscript.
Syntax
Storage_class data_type arrayname[expr1][expr2]…..[exprn];
Example
float coordinate x[4][4];
int value[10][5][5];
POINTERS
Pointer operator
A pointer operator can be represented by a combination of *(asterisk) with a
variable.
For eg.
int *ptr;
float *fp;
www.annauniversityplus.com
25
The general format of pointer declaration is…
data_type *pointer_variable;
Address operator
An address operator can be represented by a combination of & (ambersand)
with a pointer variable.
For eg.
K = &ptr;
Pointer Expressions
Pointer assignment : A pointer is a variable data type and hence the general
rule to assign its value to the pointer is same as that of any other variable
data type.
Eg.
int x,y;
int *ptr1, *ptr2;
ptr1 = &x;
The memory address of variable x is assigned to the pointer variable ptr1.
y = *ptr1;
The contents of the pointer variable ptr1 is assigned to the variable y, not the
memory address.
www.annauniversityplus.com
26
ptr1 = &x;
ptr2 = ptr1;
The address of the ptr1 is assigned to the pointer variable ptr2. The contents
of both prt1 and ptr2 will be the same as these two pointer variables hold th e
same address.
#include <iostream.h>
void main()
{
char x,y;
char *pt;
x = ‘k’; // assign character
pt = &x;
y = *pt;
cout << “Value of x = “ << x << endl;
cout << “Pointer value = “ << y << endl;
}
Structure
Eg.
www.annauniversityplus.com
27
struct student {
int rollno;
char name[20];
};
typedef
The typedef is used to define new data items that are equivalent to the
existing data types.
Syntax
Features of C++
Class
www.annauniversityplus.com
28
Class Object
A variable whose data type is a class.
Example:
class students {
int rollno;
int calculate(void);
};
Access Specifiers
Syntax:
public: <declarations>
private: <declarations>
protected <declarations>
Public
If a member is public, it can be used by any function. In C++, members
of a struct or union are public by default.
Private
If a member is private, it can only be used by member functions and
friends of the class in which it is declared. Members of a class are private by
default.
www.annauniversityplus.com
30
Protected
If a member is protected, its access is the same as for private. In
addition, the member can be used by member functions and friends of
classes derived from the declared class, but only in objects of the derived
type.
This operator can be used to refer to any member in the class explicitly.
Constructor
Firing of Constructor.
A Constructor fires at the time of creation of objects.
Types of Constructor
There are 4 types of constructors.
1. Default Constructor
2. Argument or Parametric Constructor
3. Copy Constructor
4. Dynamic Constructor
Example :
# include <iostream.h>
class date
{
private :
int dd;
int mm;
int yy;
public :
date () // ex. for default constructor
{
dd=01;
mm=01;
yy=2002;
}
date (int d,int m, int y) // ex. for argumental constructor
{
dd=d;
mm=m;
yy=y;
}
date(date x)
{
dd = x.dd;
www.annauniversityplus.com
33
mm = x.mm;
yy=x.yy;
}
void showdate();
void displaydate();
};
void date ::showdate()
{
cin >> dd>>mm>>yy;
}
void date ::displaydate()
{
cout << dd<<mm<<yy;
}
void main()
{
date d1,d2(3,3,2002);
d1.showdate();
d1.displaydate();
d2.showdate();
d2.displaydate();
date (d2); // ex for copy constructor
}
Parameterized Constructors
Inheritance
www.annauniversityplus.com
36
4. The derived class inherits all the properties of the base class and can add
properties and refinements of its own. The base class remains unchanged.
# include <iostream.h>
class Base
{
public :
void show()
{
cout << “\n Base”;
}};
class Der1 :public Base
{
public :
void show()
{
cout <<”\nDer1”;
}
};
class Der2:public Base
{
public :
void show()
{
cout <<”\nDer2”;
}
};
void main()
{
Der1 d1;
Der2 d2;
www.annauniversityplus.com
37
Base *ptr;
Ptr=&d1;
Ptr->show();
Ptr=&d2;
Ptr->show();
}
Function overloading
#include <iostream.h>
void swap(int &x, int &y);
void swap(float a,float b);
void main()
{
int x,y;
float a,b;
cout << “Enter any two integers..” << endl;
cin >> x>>y;
cout << “Enter any two Float numbers..” << endl;
cin >> a>>b;
// swapping integer numbers
swap(x,y)
cout << “After swapping integer numbers..”<<endl;
www.annauniversityplus.com
38
cout <<x<<y;
// swapping float numbers
swap(a,b)
cout << “After swapping float numbers..”<<endl;
cout <<x<<y;
}
void swap(int &a, int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void swap(float &a, float &b)
{
float temp;
temp=a;
a=b;
b=temp;
}
www.annauniversityplus.com
39
Polymorphism
A property by which we can send the same message to objects of
several different classes, and each respond in a different way depending on
its class. We can send such message without knowing to which of the
classes the objects belongs.
# include <iostream.h>
class date
{
private :
int dd;
int mm;
int yy;
public :
void showdate();
void displaydate();
};
void date ::showdate()
{
cin >> dd>>mm>>yy;
}
void date ::displaydate()
{
cout << dd<<mm<<yy;
}
void main()
{
date d;
www.annauniversityplus.com
40
d.showdate();
d.displaydate();\
}
Virtual functions
Virtual functions let derived classes provide different versions of a base class
function. You can declare a virtual function in a base class, then redefine it in
any derived class, even if the number and type of arguments are the same.
The redefined function overrides the base class function of the same name.
Virtual functions can only be member functions.
int Base::Fun(int)
and
int Derived::Fun(int)
even when they are not virtual.
The base class version is available to derived class objects via scope
override. If they are virtual, only the function associated with the actual type
of the object is available. With virtual functions, you can't change just the
function type. It is illegal, therefore, to redefine a virtual function so that it
differs only in the return type. If two functions with the same name have
different arguments, C++ considers them different, and the virtual function
mechanism is ignored.
www.annauniversityplus.com
41
objects belonging to different classes can respond to the same message in
different ways.
# include <iostream.h>
# include <string.h>
class Base
{
public :
virtual void show_message(void)
{
cout <<”Base class message”<<,endl:};
};
virtual void show_reverse(void)=0;
};
class Derived :public Base
{
public :
virtual void show_message(void)
{
cout <<”Derived class message”<<endl;
};
virtual void show_reverse(void)
{
cout <<strrev(“Derived class message”)<<endl;
}
};
void main(void)
{
Base *poly=new Derived;
Poly->show_message();
www.annauniversityplus.com
42
Poly->show_reverse();
}
Operator Overloading
C++ allows two variables of user-defined type with the same syntax that is
applied to the basic types. C++ has the ability to provide the operators with a
special meaning for a data type. The mechanism of giving such special
meaning to an operator is known as Operator Overloading.
#include <iostream.h>
class sample {
www.annauniversityplus.com
43
private :
int x;
float y;
public :
sample(int, float);
void operator = (sample abc);
void display();
};
void sample :: operator = (sample abc)
{
x = abc.x;
y = abc.y;
}
void main()
{
sample obj1;
sample obj2;
;;;;;
;;;;;
obj1 = obj2;
obj2.display();
}
Syntax:
<pointer_to_name> = new <name> [ <name_initializer> ];
delete <pointer_to_name>;
www.annauniversityplus.com
44
The "new" operator tries to create an object <name> by allocating
sizeof(<name>) bytes in the heap. The "delete" operator destroys the object
<name> by deallocating sizeof(<name>) bytes (pointed to by
<pointer_to_name>).
The storage duration of the new object is from the point of creation
until the operator "delete" deallocates its memory, or until the end of the
program.
Example:
name *nameptr; // name is any non-function type
...
if (!(nameptr = new name)) {
errmsg("Insufficient memory for name");
exit (1);
}
// Use *nameptr to initialize new name object
...
delete nameptr; //destroy name; deallocate sizeof(name) bytes
friend (keyword)
Syntax:
friend <identifier>;
class students {
friend department;
int rollno;
int calculate(void);
};
class department {
char ugcourse[25];
void performance(students*);
};
inline (keyword)
Syntax:
<datatype> <function>(<parameters>) { <statements>; }
inline <datatype> <class>::<function> (<parameters>) { <statements>; }
In C++, you can both declare and define a member function within its class.
Such functions are called inline.
The first syntax example declares an inline function by default. The syntax
must occur within a class definition.
Inline functions are best reserved for small, frequently used functions.
Example:
/* First example: Implicit inline statement */
operator (keyword)
Defines a new action
Syntax:
operator <operator symbol>( <parameters> )
{
<statements>;
}
Example:
www.annauniversityplus.com
48
Non-static member functions operate on the class type object with which they
are called. For example, if x is an object of class X and func is a member
function of X, the function call x.func() operates on x. Similarly, if xptr is a
pointer to an X object, the function call xptr->func() operates on *xptr. But
how does func know which x it is operating on? C++ provides func with a
pointer to x called "this". "this" is passed as a hidden argument in all calls to
non-static member functions.
The keyword "this" is a local variable available in the body of any nonstatic
member function. The keyword "this" does not need to be declared and is
rarely referred to explicitly in a function definition. However, it is used
implicitly within the function for member references. For example, if x.func(y)
is called, where y is a member of X, the keyword "this" i s set to &x and y is
set to this->y, which is equivalent to x.y.
Virtual Classes
You might want to make a class virtual if it is a base class that has
been passed to more than one derived class, as might happen with
multiple inheritance.
However, a base class can be indirectly passed to the derived class more
than once:
class X : public B { ... }
class Y : public B { ... }
class Z : public X, public Y { ... } // OK
www.annauniversityplus.com
49
In this case, each object of class Z will have two sub-objects of class B.
If this causes problems, you can add the keyword "virtual" to a base class
specifier.
For example,
B is now a virtual base class, and class Z has only one sub-object of class B.
Constructors for virtual base classes are invoked before any non-virtual base
classes. If the hierarchy contains multiple virtual base classes, the virtual
base class constructors are invoked in the order in which they were declared.
Any non-virtual bases are then constructed before the derived class
constructor is called. If a virtual class is derived from a non-virtual base, that
non-virtual base will be first, so that the virtual base class can be properly
constructed. For example, this code
www.annauniversityplus.com
50
Z(); // virtual base class initialization
Y(); // non-virtual base class
X(); // derived class
The compiler must be informed of the return types and parameter types of
the functions so that it will be able to check for the correct usage of the calls
to those functions during compilation. To satisfy the compiler either of the
following conventions may be adapted:
Int main(void)
{
printf(“%f\n”,square(5.0));
}
www.annauniversityplus.com
51
/* A simple C++ program to demonstrate the use of function prototype
*/
#include<iostream.h>
float square(float);
void print(float);
int main(void)
{
float x;
x=square(25.0);
print(x);
}
www.annauniversityplus.com
52
2. Default initializers and prototype definitions:
Case 1:
int power(int exponent, base); // Original declaration
int power(int exponent, base=2); // legal(successive)
redeclaration//
int power(int exponent=0, base); // legal(successive)
redeclaration//
www.annauniversityplus.com
53
Case 2:
Case 3:
Function Template
The function template acts as model and it can be replaced by any type of
function data types.
Simple Example:
#include <iostream.h>
template<class T > T add(T a, T b)
{
T c;
c=a+b;
return(c);
}
void main()
{
int a,b;
float c,d;
cin>>a>>b;
cout<<add(a,b);
cout<<add(c,d);
}
Generic Template:
A generic function defines a general set of operations that the function will apply to
various data type. A generic function receives the type of data on which the
www.annauniversityplus.com
55
function will operate as a parameter. Creating generic functions within our program
can be useful because many algrorithms are fundamentally the same in their
processing yet independent of the data type on which the algorithm operates. The
word template is a key and T type is place holder of the data type.
template <class T type> return type function name(parameter list)
{
//statement
}
#include <iostream.h>
template<class T, class T1 > T avg(T a, T b)
{
T1 c;
c=(a+b)/2;
return(c);
}
void main()
{
int a,b;
float c;
cin>>a>>b;
c=avg(a,b);
cout<<c;
}
Class Template:
If a class acts a placeholder for nay data type then the class must be defined in the
form of template.
template <class T> class someclass
{
//statement ;
www.annauniversityplus.com
56
}
Here the template not only specifies a type placeholder , it also specifies a
parameter that the program can use within the template. When the program later
uses the template, it can pass a parameter value to the template as shown here
Someclass<int 1029> this_instance
template <class T> class add
{
T a;
T b;
public:
void sum();
void getdata(); };
www.annauniversityplus.com
57
LAB CYCLE
11. Write an OOP in C++ to read a number n and print it digit by digit in
words using inline member function.
www.annauniversityplus.com
58
12. Write an OOP in C++ to read two dimensional array; find the sum of the
elements row-wise and column-wise separately, and display the sums using
‘new’ and ‘delete’ operators.
13. Develop an OOP in C++ to create a data base of the following items of
the derived class.
Name of the patient
Sex
Age
Ward number
Bed number
Nature of illness
Date of admission
Design a base class consisting of the data members, viz, name of the
patient,sex, and age and another base class consisting of ward number,bed
number and nature of the illness. The derived class consists of the data
member, date of admission. Develop the program for the following facilities.
www.annauniversityplus.com
59
17.Write a program in C++ to perform the following using the function
template concepts:
i) to read a set of integers
ii) to read a set of floating point numbers
iii) to read a set of double numbers individually.
Find out the average of the non-negative integers and also calculate the
deviation of the numbers.
www.annauniversityplus.com
60