Você está na página 1de 41

1.

Write a program to implement an Account Class with member


functions to Compute Interest, Show Balance, Withdraw and Deposit
amount from the Account.

#include<iostream.h>
#include<conio.h>
class account
{
private:
long balance;
float per;
public:
void getbalance()
{
cout<<" Enter Balance = ";
cin>>balance;
cout<<"\n Enter Interest Rate = ";
cin>>per;
}

void showbalance()
{
cout<<"\n Balance = "<<balance<<endl;
}

void interest()
{ long amt;
amt = (balance*per)/100;
cout<<"\n Interest Amount is = "<<amt<<endl;
}


void deposit()
{
long amt;
cout<<"\n How much money you want to deposit in your account = ";
cin>>amt;
balance = balance + amt ;
cout<<"\n Current Balance in Your Account = "<<balance<<endl;
}

void withdraw()
{
long amt;
cout<<"\n Enter amount you want to withdraw = ";
cin>>amt;
if(balance>=amt)
{
balance = balance - amt;
cout<<"\n Rs. "<<amt<<" is debited from your account "<<endl;
cout<<"\n Remaining Balance in your account = "<<balance<<endl;
}
else
{
cout<<"\n Insufficient Balance "<<endl;
}
}

};

void main()
{
account c;
clrscr();
c.getbalance();
c.showbalance();
c.deposit();
c.interest();
c.withdraw();
getch();
}












OUTPUT













2. Write a program in C++ to demonstrate virtual function.


#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void show()
{
cout<<"\n Base class show:";
}
virtual void display()
{
cout<<"\n Base class display:" ;
}
};

class drive:public base
{
public:
void display()
{
cout<<"\n Drive class display:";
}
void show()
{
cout<<"\n Drive class show:";
}
};

void main()
{
clrscr();
base obj1;
base *p;
cout<<"\n\t P points to base:\n" ;

p=&obj1;
p->display();
p->show();

cout<<"\n\n\t P points to drive:\n";
drive obj2;
p=&obj2;
p->display();
p->show();
getch();
}





OUTPUT













3. Write a program in C++ to create the class shape, and overload the
function to return the area of the different shapes.

#include<iostream.h>
#include<conio.h>
class shape
{
public:
int area(int);
float area(float);
int area(int,int);
float area(float,float);
};
int shape::area(int x)
{
return(x*x);
}
float shape::area(float z)
{
return(3.14*z*z);
}
int shape::area(int m,int n)
{
return(m*n);
}
float shape::area(float a,float b)
{
return(3.14*a*a*b);
}
void main()
{
clrscr();
shape ob;
int choice;
int x;
float z;
int m,n;
float a,b;
float result=0;
cout<<" Enter your choice "<<endl;
cout<<"\n 1.Area of Square"<<endl;
cout<<" 2.Area of Circle"<<endl;
cout<<" 3.Area of Rectangle"<<endl;
cout<<" 4.Area of Cylinder "<<endl;
cin>>choice;
switch(choice)
{
case 1:cout<<"\n Enter Length"<<endl;
cin>>x;
result = ob.area(x);
break;
case 2:cout<<"\n Enter Radius"<<endl;
cin>>z;
result = ob.area(z);
break;
case 3:cout<<"\n Enter Length =";
cin>>m;
cout<<" Enter Breadth = ";
cin>>n;
result = ob.area(m,n);
break;
case 4:cout<<"\n Enter Radius = ";
cin>>a;
cout<<" Enter Height = ";
cin>>b;
result = ob.area(a,b);
break;
default:cout<<"\t \t****Invalid Input****"<<endl;
break;
}
cout<<"\n Result : "<<result<<endl;
getch();
}










OUTPUT













4. Write a program to resolve the ambiguity of multiple inheritance in
C++?

#include<iostream.h>
#include<conio.h>
class product
{
private:
int productid;
char productname[30];
public:
void getproduct()
{
cout<<" Enter product id = ";
cin>>productid;
cout<<" Enter product name = ";
cin>>productname;
}

void showproduct()
{
cout<<"\n Product Id = "<<productid<<"\n Product Name = "<<productname<<endl;
}
};

class unitone : public virtual product
{
protected:
int stockone;
public:
void getone()
{
cout<<"\n Enter quantity of Stock in Unit One = ";
cin>>stockone;
}

void showone()
{
cout<<"\n Quantity of Stock in Unit One = "<<stockone<<endl;
}
};

class unittwo : public virtual product
{
protected:
int stocktwo;
public:
void gettwo()
{
cout<<" Enter Quantity of Stock in Unit Two = ";
cin>>stocktwo;
}

void showtwo()
{
cout<<" Quantity of Stock in Unit Two = "<<stocktwo<<endl;
}
};

class totalunit : public unitone,public unittwo
{
private:
int total;
public:
void gettotal()
{
getproduct();
getone();
gettwo();
total=stockone + stocktwo ;
}

void showtotal()
{
showproduct();
showone();
showtwo();
cout<<"\n Total Product in both Unit = "<<total<<endl;
}
};

void main()
{
totalunit obj;
clrscr();
obj.gettotal();
obj.showtotal();
getch();
}
OUTPUT







5. Write a program in C++ to demonstrate unary operator over complex
number class.

#include<iostream.h>
#include<conio.h>
class complex
{
private:
int x,y,z;
public:
complex()
{}

complex(int ,int );

void getdata()
{
cout<<"\n Enter First Numbers = ";
cin>>x;
cout<<"\n Enter Second Number = ";
cin>>y;
}
void operator++()
{
x=++x;
y=++y;
}

void showdata()
{
cout<<x<<"+"<<y<<"i"<<endl;
}
}; complex::comples(int a,int b)
{
x=a;
y=b;
}
void main()
{
clrscr();
complex obj;
complex(obj.getdata();)
obj.getdata();

cout<<"\n Entered Complex Number = ";
obj.showdata();
++obj;
cout<<"\n Number After Increment = ";
obj.showdata();
getch();
}









OUTPUT















6. Create a class Distance, which accepts data in feet and inches,
adds two distances and displays the members of the distance object
in the appropriate form. Test the class in the main program by
creating object d1 and d2 of type distance, accept data for each
object and add tem then display them.


#include<iostream.h>
#include<conio.h>
class distance
{
int feet,inch;
public:
distance()
{
feet=0;
inch=0;
}
void getdata()
{
cout<<"\n Enter Feet = ";
cin>>feet;
cout<<" Enter Inch = ";
cin>>inch;
}
distance(int a,int b)
{
feet=a,inch=b;
}
void showdata()
{
cout<<"\n "<< feet<<" Feets "<<inch<<" Inches \n"<<endl;
}
distance operator +(distance d)
{
distance t;
t.feet=feet+ d.feet;
t.inch=inch+d.inch;
t.feet=t.feet+(t.inch/12);
t.inch=t.inch%12;
return(t);
}
};
void main()
{
clrscr();
distance d1,d2,d3;
d1.getdata();
d1.showdata();
d2.getdata();
d2.showdata();
d3=d1+d2;
cout<<" After Addition "<<endl;
d3.showdata();
getch();
}

OUTPUT










7. Write a program to demonstrate External, automatic, static and
dynamic objects?

#include<iostream.h>
#include<conio.h>
class object
{
private:
int value;
public:
object();

void addvalue(int value1);

int getvalue();
};

void object::object()
{
value = 12;
}

void object::addvalue(int value1)
{
value = value + value1;
}

int object::getvalue()
{
return(value);
}

object externalobj;

void main()
{
externalobj.addvalue(10);
clrscr();
for(int i=0;i<2;i++)
{
object autoobj;
autoobj.addvalue(15);
static object statobj;
statobj.addvalue(20);

cout<<"\n \n Auto object value = "<<autoobj.getvalue();
cout<<"\n\n Static object value = "<<statobj.getvalue();
}

cout<<"\n\n External object value is = "<<externalobj.getvalue();
getch();
}



OUTPUT










8. Write a program in C++ to create a file and append the content of
the file. (Assume suitable data).

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class student
{
private :
char name[50];
public:
void getdata()
{
cout<<" Product Name = ";
cin>>name;
}
void showinfo()
{
cout<<"\n Product name = "<<name;
}

}s1;

void main()
{
ofstream my;
my.open("MYSTYLE.txt",ios::app);
clrscr();
char ans = 'y';
while(ans == 'y')
{
s1.getdata();
my.write((char*)&s1,sizeof(s1));
cout<<"\n Record Added To file. \n "<<endl;
cout<<" Want to Enter More Records ? (y/n)... "<<endl;
cin>>ans;
}
my.close();

}
















OUTPUT













9. Write a program to demonstrate the concept of Meta-class?

#include<iostream.h>
#include<conio.h>
class bank
{
private:
int acno;
char name[30];
long balance;
static long bankbalance;
public:

void openacc()
{
cout<<"\n Enter Balance = ";
cin>>balance;
bankbalance=bankbalance + balance;
}
static void show()
{
cout<<"\n Bank balance = "<<bankbalance;
}
};

long bank:: bankbalance;
void main()
{
bank c1,c2,c3;
clrscr();
c1.openacc();
c2.openacc();
c3.openacc();
bank::show();
getch();
}


OUTPUT



10. Firstly, create a file which contains some integer values. Then,
write a program to read the data in sequential mode from the file,
find the total of the given integer values and display the result.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<fstream.h>
void main()
{
clrscr();
float a[]={12,5,23,7};
fstream fout;
fout.open("mystylepro.txt",ios::in|ios::out);
for(int i =0 ;i<4; i++)
{
fout<<a[i]<<"\n";
}
fout.close();
for(i=0;i<4;i++)
{
a[i]=0;
}
fstream fin;
fin.open("mystylepro.txt",ios::in|ios::out);
int h[4];int sum=0;
for(i=0;i<4;i++)
{
fin>>a[i];
h[i]=a[i];
sum=sum+h[i];
}
cout<<"\n The Sum of the Integer is = "<<sum;
getch();
}






OUTPUT








Experiment 11

Explain Class diagram and Object diagram with
the help of UML diagram?


Overview:
The class diagram is a static diagram. It represents the static view of an
application. Class diagram is not only used for visualizing, describing and
documenting different aspects of a system but also for constructing executable
code of the software application.
The class diagram describes the attributes and operations of a class and also
the constraints imposed on the system. The class diagrams are widely used in
the modelling of object oriented systems because they are the only UML
diagrams which can be mapped directly with object oriented languages.
The class diagram shows a collection of classes, interfaces, associations,
collaborations and constraints. It is also known as a structural diagram.

Purpose:
The purpose of the class diagram is to model the static view of an application.
The class diagrams are the only diagrams which can be directly mapped with
object oriented languages and thus widely used at the time of construction.
The UML diagrams like activity diagram, sequence diagram can only give the
sequence flow of the application but class diagram is a bit different. So it is the
most popular UML diagram in the coder community.
So the purpose of the class diagram can be summarized as:
Analysis and design of the static view of an application.
Describe responsibilities of a system.
Base for component and deployment diagrams.
Forward and reverse engineering.

How to draw Class Diagram?
Class diagrams are the most popular UML diagrams used for construction of
software applications. So it is very important to learn the drawing procedure of
class diagram.
Class diagrams have lot of properties to consider while drawing but here the
diagram will be considered from a top level view.
Class diagram is basically a graphical representation of the static view of the
system and represents different aspects of the application. So a collection of
class diagrams represent the whole system.
The following points should be remembered while drawing a class diagram:
The name of the class diagram should be meaningful to describe the
aspect of the system.
Each element and their relationships should be identified in advance.
Responsibility (attributes and methods) of each class should be clearly
identified.
For each class minimum number of properties should be specified.
Because unnecessary properties will make the diagram complicated.
Use notes whenever required to describe some aspect of the diagram.
Because at the end of the drawing it should be understandable to the
developer/coder.
Finally, before making the final version, the diagram should be drawn on
plain paper and rework as many times as possible to make it correct.
Now the following diagram is an example of an Order System of an application.
So it describes a particular aspect of the entire application.
First of all Order and Customer are identified as the two elements of the
system and they have a one to many relationship because a customer
can have multiple orders.
We would keep Order class is an abstract class and it has two concrete
classes (inheritance relationship) Special-order and Normal Order.
The two inherited classes have all the properties as the Order class. In
addition they have additional functions like dispatch () and receive ().


Where to use Class Diagrams?
Class diagram is a static diagram and it is used to model static view of a system.
The static view describes the vocabulary of the system.
Class diagram is also considered as the foundation for component and
deployment diagrams. Class diagrams are not only used to visualize the static
view of the system but they are also used to construct the executable code for
forward and reverse engineering of any system.
Generally UML diagrams are not directly mapped with any object oriented
programming languages but the class diagram is an exception.
Class diagram clearly shows the mapping with object oriented languages like
Java, C++ etc. So from practical experience class diagram is generally used for
construction purpose.
So in a brief, class diagrams are used for:
Describing the static view of the system.
Showing the collaboration among the elements of the static view.
Describing the functionalities performed by the system.
Construction of software applications using object oriented languages.







Overview:
Object diagrams are derived from class diagrams so object diagrams are
dependent upon class diagrams.
Object diagrams represent an instance of a class diagram. The basic concepts
are similar for class diagrams and object diagrams. Object diagrams also
represent the static view of a system but this static view is a snapshot of the
system at a particular moment.
Object diagrams are used to render a set of objects and their relationships as
an instance.

Purpose:
The purpose of a diagram should be understood clearly to implement it
practically. The purposes of object diagrams are similar to class diagrams.
The difference is that a class diagram represents an abstract model consisting
of classes and their relationships. But an object diagram represents an instance
at a particular moment which is concrete in nature.
It means the object diagram is more close to the actual system behaviour. The
purpose is to capture the static view of a system at a particular moment.
So the purpose of the object diagram can be summarized as:
Forward and reverse engineering.
Object relationships of a system
Static view of an interaction.
Understand object behaviour and their relationship from practical
perspective

How to draw Object Diagram?
We have already discussed that an object diagram is an instance of a class
diagram. It implies that an object diagram consists of instances of things used
in a class diagram.
So both diagrams are made of same basic elements but in different form. In
class diagram elements are in abstract form to represent the blue print and in
object diagram the elements are in concrete form to represent the real world
object.
To capture a particular system, numbers of class diagrams are limited. But if
we consider object diagrams then we can have unlimited number of instances
which are unique in nature. So only those instances are considered which are
having impact on the system.
From the above discussion it is clear that a single object diagram cannot
capture all the necessary instances or rather cannot specify all objects of a
system. So the solution is:
First, analyse the system and decide which instances are having
important data and association.
Second, consider only those instances which will cover the functionality.
Third, make some optimization as the numbers of instances are
unlimited.
Before drawing an object diagrams the following things should be remembered
and understood clearly:
Object diagrams are consist of objects.
The link in object diagram is used to connect objects.
Objects and links are the two elements used to construct an object
diagram.
Now after this the following things are to be decided before starting the
construction of the diagram:
The object diagram should have a meaningful name to indicate its
purpose.
The most important elements are to be identified.
The association among objects should be clarified.
Values of different elements need to be captured to include in the object
diagram.
Add proper notes at points where more clarity is required.
The following diagram is an example of an object diagram. It represents
the Order management system which we have discussed in Class Diagram. The
following diagram is an instance of the system at a particular time of purchase.
It has the following objects
Customer
Order
Special-order
Normal Order
Now the customer object (C) is associated with three order objects (O1, O2 and
O3). These order objects are associated with special order and normal order
objects (S1, S2 and N1). The customer is having the following three orders with
different numbers (12, 32 and 40) for the particular time considered.
Now the customer can increase number of orders in future and in that scenario
the object diagram will reflect that. If order, special order and normal order
objects are observed then we you will find that they are having some values.
For orders the values are 12, 32, and 40 which implies that the objects are
having these values for the particular moment (here the particular time when
the purchase is made is considered as the moment) when the instance is
captured.
The same is for special order and normal order objects which are having
number of orders as 20, 30 and 60. If a different time of purchase is considered
then these values will change accordingly.
So the following object diagram has been drawn considering all the points
mentioned above:




Where to use Object Diagrams?
Object diagrams can be imagined as the snapshot of a running system at a
particular moment. Now to clarify it we can take an example of a running train.
Now if you take a snap of the running train then you will find a static picture of
it having the following:
A particular state which is running
A particular number of passengers. Which will change if the snap is
taken in a different time?
So here we can imagine the snap of the running train is an object having the
above values. And this is true for any real life simple or complex system. In a
brief, object diagrams are used for:
Making the prototype of a system.
Reverse engineering.
Modelling complex data structures.
Understanding the system from practical perspective.







Experiment 12

How will you show association, aggregation,
composition and multiplicity in a class diagram?


Association
An association represents a family of links. Binary associations (with two ends)
are normally represented as a line. An association can be named, and the ends
of an association can be adorned with role names, ownership indicators,
multiplicity, visibility, and other properties.
There are four different types of association: bi-directional, uni-directional,
Aggregation (includes Composition aggregation) and Reflexive. Bi-directional
and uni-directional associations are the most common ones.
For instance, a flight class is associated with a plane class bi-directionally.
Association represents the static relationship shared among the objects of two
classes. Example: "department offers courses", is an association relation.



Aggregation
Aggregation is a variant of the "has an" association relationship; aggregation is
more specific than association. It is an association that represents a part-whole
or part-of relationship. As a type of association, an aggregation can be named
and have the same adornments that an association can. However, an
aggregation may not involve more than two classes.
Aggregation can occur when a class is a collection or container of other
classes, but where the contained classes do not have a strong life cycle
dependency on the containeressentially, if the container is destroyed, its
contents are not.
In UML, it is graphically represented as a hollow diamond shape on the
containing class end of the tree with a single line that connects the contained
class to the containing class. The aggregate is semantically an extended object
that is treated as a unit in many operations, although physically it is made of
several lesser objects.



Composition
Composition is a stronger variant of the "has a" association relationship;
composition is more specific than aggregation.
Composition usually has a strong life cycle dependency between instances of
the container class and instances of the contained class(es): If the container is
destroyed, normally every instance that it contains is destroyed as well. (Note
that, where allowed, a part can be removed from a composite before the
composite is deleted, and thus not be deleted as part of the composite.)

The UML graphical representation of a composition relationship is
a filled diamond shape on the containing class end of the tree of lines that
connect contained class(es) to the containing class.


Multiplicity
This association relationship indicates that (at least) one of the two related
classes make reference to the other. This relationship is usually described as "A
has a B" (a mother cat has kittens, kittens have a mother cat).
The UML representation of an association is a line with an optional arrowhead
indicating the role of the object(s) in the relationship, and an optional notation
at each end indicating the multiplicity of instances of that entity (the number
of objects that participate in the association).

Você também pode gostar