Tuesday, November 24, 2015

template programs

#include<iostream.h>
#include<conio.h>
template <class T>
T add(T a, T b)
{
T c;
c=a+b;
return(c);
}
template < class T>
void print (T a, int b)
{
for(int i=0;i<b;i++)
{cout<<a;}

}

template < class T>
T max( T a, T b, T c)
{
if(a>b && a>c)
{return(a);}

if(b>c && b>c)
{return(b);}
if(c>a && c>b )
{return(c);}

}


void main()
{
clrscr();
cout<<add(4,5);
cout<<add(3.4,4.5);
cout<<add('A','B');
print('%',3);
print("&&",2);
cout<<max(9,6,56);
cout<<max('A', 'D', 'B');
getch();
}

===============================================


#include<iostream.h>
#include<conio.h>
template <class T>
class Array
{
T *A;
int sz;
public:
Array(int n)
{
sz=n;
}
//template <class T>
void getdata()
{
int i;
for(i=0;i<sz;i++)
{
cin>> A[i];
}
}
//template <class T>
void disp()
{
int i;
for(i=0;i<sz;i++)
{
cout<< A[i];
}
}
} ;


void main()
{
clrscr();
Array <int> Obj1(3);
Array <char> Obj2(4);
Obj1.getdata();
Obj1.disp();
Obj2.getdata();
Obj2.disp();
getch();
}

generic programming

Generic programming is about generalizing software components so that they can be easily reused in a wide variety of situations. In C++, class and function templates are particularly effective mechanisms for generic programming because they make the generalization possible without sacrificing efficiency.


Generic programming means that you are not writing source code that is compiled as-is but that you write "templates" of source codes that the compiler in the process of compilation transforms into source codes.


The goal of generic programming is to write code that is independent of the data types. In C language, all codes are tied to a specific data type. For container data structures (such as array and structure), you need to specify the type of the elements. For algorithms (such as searching or sorting), the code works only for a specific type, you need to rewrite the code for another type. Can we write a single sorting routine that works on all types (or most of the types) by specifying the type during invocation? Can we have a general container that can work on all types?
Template lets you program on generic type, instead of on a specific type. Template supports so-called parameterized type - i.e., you can use type as argument in building a class or a function (in class template or function template). Template is extremely useful if a particular algorithm is to be applied to a variety of types, e.g., a container class which contains elements, possibly of various types.

Function Template

function template is a generic function that is defined on a generic type for which a specific type can be substituted. Compiler will generate a function for each specific type used. Because types are used in the function parameters, they are also called parameterized types.
The syntax of defining function template is:
template <typename T> OR template <class T>
return-type function-name(function-parameter-list) { ...... }

Class Template

The syntax for defining a class template is as follow, where T is a placeholder for a type, to be provided by the user.
template <class T>    // OR template <typename T>
class ClassName {
   ......
}
The keywords class and typename (newer and more appropriate) are synonymous in the definition of template.
To use the template defined, use the syntax ClassName<actual-type>.

Friday, November 20, 2015

Virtual Functions Programs:I II III

#include<iostream.h>
class Base
{
public:
int b;
void show()
{
cout<<"\n Hello I m in Base";
}
};

class Derv:public Base
{
public:
int d;
void show()
{
cout<<"\n Hello I m in Derv";
}
}  ;


int main()
{
Base B, *bptr;
Derv D,*dptr;

//CASE I
bptr=&B;
bptr->show(); // In Base

//CASE II
bptr=&D;
bptr->show(); // In Base

//CASE III
//dptr=&B; //Not OK, cant convert

//CASE IV
dptr=&D;
dptr->show();           //In Derv

}



==============================
#include<iostream.h>
class Base
{
public:
int b;
void show()
{
cout<<"\n Hello I m in Base show";
}
virtual void disp()
{
cout<<"\n Hello I m in Base disp";
}
virtual void display()
{
cout<<"\n Hello I m in Base display";
}
};
class Derv:public Base
{
public:
int d;
void show()
{
cout<<"\n Hello I m in Derv show";
}
void disp()
{
cout<<"\n Hello I m in Derv disp";
}
}  ;


int main()
{
Base B, *bptr;
Derv D,*dptr;
B.show();        //Base
B.disp();       // Base
B.display();    //Base

D.show();        //Derv
D.disp();       //Derv
D.display();    //Base


bptr=&B;
bptr->show(); // In Base
bptr->disp(); // In Base //////********
bptr->display();  // In Base

bptr=&D;
bptr->show(); // In Base
bptr->disp(); // In Derv   ////////*********
bptr->display(); // In Base

//////*****This is run time polymorphism
}
=================


#include<iostream.h>
class Figure
{
public:
virtual void showarea(){};
};
class Tri:public Figure
{
public:
void showarea()
{
cout<<"\n Hello I m in Triangle";
}
}  ;


class Rect:public Figure
{
public:
void showarea()
{
cout<<"\n Hello I m in Rectangle";
}
}  ;


class Square:public Figure
{
public:
void showarea()
{
cout<<"\n Hello I m in Square";
}
}  ;


int main()
{
Figure *fptr[3];
Tri T;
Rect R;
Square S;
fptr[0]=&T;
fptr[1]=&R;
fptr[2]=&S;
for(int i=0;i<3;i++)
{
fptr[i]->showarea();
}
}

Virtual Functions

Virtual Function
A Virtual function is a function which is declared in base class using the keyword virtual. We write the body of virtual function in the derived classes. Its purpose is to tell the compiler that what function we would like to call on the basis of the object of derived class. C++ determines which function to call at run time on the type of object pointer to.
=============
The way to retain the behavior of the object’s instantiated type is through the use of virtual functions. To declare a method to be a virtual function, you simply use the virtual keyword when declaring the method in the class definition. When you call a function, it will check the dynamic type of the object before choosing which function to call—this process is called reification.

It is important that you declare the function to be virtual throughout your class hierarchy, or its behavior will be quite unexpected. A virtual method can call a nonvirtual method and vice-versa. Overloaded operator functions can be virtual functions, but static methods can’t be.
A constructor cannot be a virtual function because it needs to know the exact type to create. However, a destructor can be declared as virtual, and generally should be. virtual functions may not seem significant at first, but they enable a ton of code reuse when using class hierarchies. und Circle, Shape, and Rectangle objects, the client doesn’t have to as long as all of the relevant functions are virtual==========
===================

Thursday, November 19, 2015

Phase IV for BCA III

Sub Name: Object Oriented Programming Using UML & C++
Sub Code: TBC 302
PHASE IV

CONSTRUCTOR
Q1. WAP to demonstrate the use of Different types of Constructors in class NUMBER
NUMBER ()
NUMBER (int a)
NUMBER (int a int b)
NUMBER (NUMBER & A)

Q2. WAP to demonstrate the use of Destructor in class NUMBER
~ NUMBER ()
Q3. Create a class called DISTANCE that has separate member data inches and feet. One constructor should initialize this data to 0, and another should initialize it to fixed values. A member function should display it. The member function should add two objects of type distance passed as arguments. A main ( ) program should create two initialized distance objects, and one that isn’t initialized. Then it should add the two initialized values together, leaving the result in the third distance variable. Finally display the third variable.
INHERITANCE
Q4.  WAP to show multiple inheritance eg, person, faculty and student class.
Q4.  Create two classes DM and DF, which store the value of distances. DM stores distances in meters and centimeters and DF in feet and inches. Write a program that can read values for the class object and add one object of DM with another object of DF. Use a friend function to carry out the addition operation. The object that stores the results may be a DM object or DF object, depending on the units in which results are required. The display should be in the format of feet and inches or meters and centimeters depending on the object on display.
Q5.Write a program to read and display information about employees and managers. Employee is a class that contains employee number, name, address and department. Manager class contains all information of the employee class and list of employees working under manager.
Q6. What is virtual function and how it is used to implement late binding? How will you make a class as an abstract class?
a)WAP to design three classes Figure, Line, Square
Figure:
Name
Color
virtual Getdata()
Disp()
Line:
Name
Color
Getdata()
Disp()
Square:
Name
Color
Side
Area
Getdata()
Disp()
Calc_Area()
Q7. WAP to design three classes Figure, Line, Square.
Figure:
Name
Color
virtual Getdata() =0;
Disp()
Line:
Name
Color
Getdata()
Disp()
Square:
Name
Color
Side
Area
Getdata()
Disp()
Calc_Area()
GENERIC  PROGRAMMING
Q8.  Write a C++ program to create a template function for BUBBLE_SORT and demonstrate sorting of integers and doubles.
Q9.  Write a Function Template for the FIND_MAX function, that finds max of three int, three float and three double value types
Q10.Write a Class Template for the array that can have int, float and double value types as data members.






Phase III qus for BCA III

Sub Name: Object Oriented Programming Using UML & C++
Sub Code: TBC 302
PHASE III

FUNCTION OVERLOADING:
Q 1. Write a program that use function overloading to do the following task-
n  Demonstrate the exact match
n  Demonstrate the integral promotion
n  Demonstrate the ambiguity error
n  Demonstrate the concept of default arguments along with function overloading
Q2. Write a program that use function overloading to do the following task-
n  Find the max. of two numbers
n  Find the max of three numbers
Q3. Write a program that use function overloading to do the following task-
n  Compute xy , where x and y both are of int type. 
n  Compute xy,  where x is float type and y is int type.
Q4. Write a C++ program to create a class called COMPLEX and implement the following overloading functions ADD that return a complex number:
a. ADD(a, s2) – where ‘a’ is an integer (to be added to real part only) and s2 is a complex number
b. ADD(s1, s2) – where s1 and s2 are complex numbers
c. ADD(s1, a) – where ‘a’ is an integer (to be added to real part as well as imaginary part) and s1 is a complex number
Q5. Demonstrate the concept of function overloading among the classes by designing three classes Square, Rectangle, & Circle. All the three classes must have functions getdata(), calc_area(), disp();
Operator Overloading
Q6. Write a complete definition for an overloaded + , - , * operator for the INTEGER class. It should add 2 INTEGER objects. It can solve the following expressions:
A=B*C
A=B*8;
A=B+C-D
A=B-C*D/E
Q7 A class Clock has following members:
Data members:
            Hour of type integer
            Minute of type integer
Second  of type integer
Member function:
            Readtime (int h, int m, int s);
            Showtime ();
            addTime(Clock);
addTime(Clock,Clock);
Write a complete program in C++ to input two different objects FT, ST. Print their sum (assuming 24 hour clock time) by overloading the + operator for class Clock
Q8. Write a complete definition for an overloaded + , - , * operator for the COMPLEX class. It should add 2 COMPLEX objects. It can solve the following expressions:
A=B*C
A=B+C
A=B*8;
A=A*B+C-5*D+8
A=8*B;
If some operation is not possible, mention the reason.
Q9. WAP to add one INTEGER with one FLOAT object, using overloading binary + operator using friend function for FLOAT class and member function for INTEGER class.
Q10. WAP to overload += operator overloading.
Q11. WAP to overload ++ operator overloading.(prefix & postfix)
Q12. WAP to overload -- operator overloading. (prefix & postfix)
Q13. WAP to overload == operator overloading.
Q14. WAP to overload << operator overloading.

Q15. WAP to overload >> operator overloading.

Friday, September 18, 2015

State Diagram

State Diagrams: State Diagrams State diagrams are created during the analysis and design phase to describe the behaviour of nontrivial objects. State diagrams are good for describing the behaviour of one object across several use cases and are used to identify object attributes and to refine the behaviour description of an object.
There are three major components of a state diagram:
State:
A state is a condition in which an object can be at some point during its lifetime, for some finite period of. State diagrams describe all the possible states a particular object can get into and how the objects state changes as a result of external events that reach the object.
States are represented by the values of the attributes of an object.
A state represents a stage in the behaviour pattern of an object, and in a state diagram it is possible to have initial states and final states.
An initial state, also called a creation state, is the one that an object is in when it is first created, whereas a final state is one in which no transitions lead out of.
. In a state diagram:
• A state is represented by a rounded rectangle.
 • A start state is represented by a solid circle.
• A final state is represented by a solid circle with another open circle around it.
Transition
A transition is a progression from one state to another and will be triggered by an event that is either internal or external to the object.
Transitions are the result of the invocation of a method that causes an important change in state.
 A transition is a change of an object from one state (the source state) to another (the target state) triggered by events, conditions, or time. Transitions are represented by an arrow connecting two states.
Transitions can also be labeled with guards (a Boolean expression which evaluates to true or false) inside square brackets, such as [trade accepted]. A guarded transition occurs only if the guard resolves to true. Only one transition can be taken out of a given state. If more than one guard condition is true, only one transition will fire. The choice of transition to fire is nondeterministic if no priority rule is given
The arrows in state diagram represent transitions, progressions from one state to another.
Event: Is something that occurs at a point of time.
Events are internal or external factors influencing the system.
Event causes the transitions

State diagrams are used to model states and also events operating on the system.