1424589443.9399LAB 4

advertisement
LAB 3
1. Exercise 1: Class Point
This exercise consists to create the class Point, which must have two private double
type variables, x and y and the necessary functions so that the following statements can
be executed:
#include "stdafx.h"
#include<iostream>
using namespace std;
class Point
{
private:
double x,y;
public:
Point ();
Point(double a, double b);
Point(double a);
void Translate(double dx, double dy);
void Display();
double Get_x();
double Get_y();
};
Point::Point ()
{
x=0.0;
y=0.0;
}
Point::Point(double a, double b)
{
x=a;
y=b;
}
Point::Point(double a)
{
x=a;
y=0.0;
}
void Point::Translate(double dx, double dy)
{
x+=dx; //x=x+dx;
y+=dy; // y=y+dy;
}
void Point::Display()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
double Point::Get_x()
{ return x;}
double Point::Get_y()
{ return y;}
int main()
{
Point P1;
cout<<"Point P1 before translation=("<<P1.Get_x()<<","<<P1.Get_y()<<")"<<endl;
P1.Translate(4.75,2.5);
cout<<"Point P1 after translation=("<<P1.Get_x()<<","<<P1.Get_y()<<")"<<endl;
Point P2(2.5,1.75);
cout<<"Point P2 before translation=("<<P2.Get_x()<<","<<P2.Get_y()<<")"<<endl;
1
P2.Translate(0.75,1.75);
cout<<"Point P2 after translation=("<<P2.Get_x()<<","<<P2.Get_y()<<")"<<endl;
Point P4(1.75);
cout<<"Point P4 before translation=("<<P4.Get_x()<<","<<P4.Get_y()<<")"<<endl;
P4.Translate(0.75,1.75);
cout<<"Point P4 after translation=("<<P4.Get_x()<<","<<P4.Get_y()<<")"<<endl;
Point *P3=new Point(1.5,1.25);
cout<<"Point P3 before translation=("<<P3->Get_x()<<","<<P3->Get_y()<<")"<<endl;
P3->Translate(0.75,1.75);
cout<<"Point P3 after translation=("<<P3->Get_x()<<","<<P3->Get_y()<<")"<<endl;
cout<<"\n \n \n *****Display 4 Point with display function****"<<endl;
cout<<"\n \n";
cout<<"Point P1\n";
P1.Display();
cout<<"\n \n";
cout<<"Point P2\n";
P2.Display();
cout<<"\n \n";
cout<<"Point P3\n";
P3->Display();
cout<<"\n \n";
cout<<"Point P4\n";
P4.Display();
cout<<"\n \n";
return 0;
}
2. Exercise 2: Class Student
This exercise consists of making a program to manage “students” in a database. For
that, you must start with the class Student given below. It has to contain just two
members functions: prt() and get(), which will allow to ask for the name and the
average mark. You have to implement the functions in the file student.cpp.
// kk.cpp : Defines the entry point for the console application.
//
// stud.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define NUM 5
#include <iostream>
#include <string.h>
using namespace std;
class Student
{
char name[20];
double average;
public:
Student(char *n,double a);
void prt();
void get();
public:
~Student();
};
Student::Student(char *n="",double a=5.0)
{
2
//name=new char[20];
strcpy(name,n);
average=a;
}
Student::~Student()
{
cout<<" delete object "<< endl;
}
void Student::prt()
{
cout<<" name= "<< name <<" average= "<<average
}
void Student::get()
{
cout<<"Enter the name"<<endl;
cin>>name;
cout<<"Enter the average"<<endl;
cin>>average;
}
<<endl;
int main()
{
int num;
cout << "Static"<<endl;
Student vec[NUM];
for(int i=0;i<NUM;i++)
{
vec[i].get();
}
cout << "Data:"<<endl;
for(int i=0;i<NUM;i++)
vec[i].prt();
cout << "Dynamic"<<endl;
cout << "Enter number of students:";
cin >> num;
Student *pv = new Student[num];
for(int i=0;i<num;i++)
pv[i].get();
cout << "Data:"<<endl;
for(int i=0;i<num;i++)
pv[i].prt();
delete [] pv;
return 0;
}
3. Exercise 3: Bank Account
This exercise consists of making use of static variables, or class variables, which are
the same for all objects.
You must add the necessary code so you can perform the following operations with the
accounts: Add Balance, Interest Payment, Payments, Transfers from one account to
another, etc.
3
// Banque.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
using namespace std;
class BankAcc
{
double balance;
public:
BankAcc (double b=0){ balance=b; }
void AddBalance(double quant);
void Payment(double amount);
double Balance(){ return balance;}
bool Transfer (double amount, BankAcc & c);
};
void BankAcc::AddBalance(double quant)
{
balance+=quant;
}
void BankAcc::Payment(double amount)
{
balance-=amount;
}
bool
{
BankAcc:: Transfer (double amount, BankAcc & c)
if(balance>amount)
{
balance-=amount;
c.balance+=amount;
return true;
}
else
return false;
}
int main()
{
BankAcc acc1(100), acc2(200);
cout << "Acc1:" <<acc1.Balance()<< "Acc2:" <<acc2.Balance()<< endl;
acc1.Payment(50); acc2.Payment(20);
cout << "Acc1:"<<acc1.Balance()<< "Acc2:"<< acc2.Balance()<<endl;
if ( acc1.Transfer (80, acc2) == true){
cout << "Trans. made"<<endl;
}else{
cout << "No balance.."<<endl;
acc1.AddBalance(100);
}
cout << "Acc1:" <<acc1.Balance()<<"Acc2:"<< acc2.Balance() << endl;
return 0;
}
4
Download