当前位置:网站首页>Experiment 5 constructor and destructor
Experiment 5 constructor and destructor
2022-06-12 00:12:00 【Jun Yehan】
Experiment five Constructors and destructors
A. Point( Class and construction )
Title Description
The following is the class definition of a point on a plane , Please implement all its methods outside the class , And generate points to test it .
Input
Number of groups of test data t
The first set of test data points p1 Of x coordinate The first set of test data points p1 Of y coordinate The first set of test data points p2 Of x coordinate The first set of test data points p2 Of y coordinate
…
Output
Output p1 To p2 Distance of
stay C++ in , The reference code for outputting the specified precision is as follows :
#include
#include // Must contain this header file
using namespace std;
void main( )
{ double a =3.141596;
cout<<fixed<<setprecision(3)<<a<<endl; // Output after decimal point 3 position
}
sample input
2
1 2 3 4
-1 0.5 -2 5
sample output
Distance of Point(1.00,2.00) to Point(3.00,4.00) is 2.83
Distance of Point(-1.00,0.50) to Point(-2.00,5.00) is 4.61
Reference code
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
class Point
{
double x,y;
public:
Point();// Default constructor
Point(double x_value,double y_value);// There are parameter constructors
double getX();// return x Value
double getY();// return y Value
void setX(double x_value);
void setY(double y_value);
double distanceToAnotherPoint(Point p);
};
Point::Point()
{
x=0;
y=0;
}
Point::Point(double x_value,double y_value)
{
x=x_value;
y=y_value;
}
void Point::setX(double x_value)
{
x=x_value;
}
void Point::setY(double y_value)
{
y=y_value;
}
double Point::getX()
{
return x;
}
double Point::getY()
{
return y;
}
double Point::distanceToAnotherPoint(Point p)
{
double len_x;
double len_y;
double len;
len_x=x-p.x;
len_y=y-p.y;
len=sqrt(len_x*len_x+len_y*len_y);
return len;
}
int main()
{
int t;// Number of test groups
Point p1,p2;
double x1,y1,x2,y2,dis;
cin>>t;
while(t--)
{
cin>>x1>>y1>>x2>>y2;
// Set the coordinates of the two points
p1.setX(x1);
p1.setY(y1);
p2.setX(x2);
p2.setY(y2);
dis=p1.distanceToAnotherPoint(p2);
cout<<"Distance of Point("<<fixed<<setprecision(2)<<p1.getX()<<','<<fixed<<setprecision(2)<<p1.getY()<<')';
cout<<" to Point("<<fixed<<setprecision(2)<<p2.getX()<<','<<fixed<<setprecision(2)<<p2.getY()<<')';
cout<<" is "<<fixed<<setprecision(2)<<dis<<endl;
}
}
B. Date( Class and construction )
Title Description
The following is the definition of a date class , Please implement all its methods outside the class , And generate the object test in the main function .
Be careful , When judging tomorrow's date , To join the cross month 、 straddle old and new years 、 Leap year judgment
for example 9. month 30 Tomorrow is 10 month 1 Japan ,12 month 31 Tomorrow is the second year 1 month 1 Japan
2 month 28 Tomorrow of the day should distinguish whether it is a leap year , Leap years are 2 month 29 Japan , Non leap years are 3 month 1 Japan
Input
Number of groups of test data t
Year of the first set of test data month Japan
…
The first date is required to be initialized with a constructor , The initialization of the second date adopts setDate Method , The third date uses the constructor , The fourth date adopts setDate Method , And so on .
Output
Output today's date
Output tomorrow's date
sample input
4
2012 1 3
2012 2 28
2012 3 31
2012 4 30
sample output
Today is 2012/01/03
Tomorrow is 2012/01/04
Today is 2012/02/28
Tomorrow is 2012/02/29
Today is 2012/03/31
Tomorrow is 2012/04/01
Today is 2012/04/30
Tomorrow is 2012/05/01
Reference code
#include<iostream>
#include<iomanip>
using namespace std;
class Date
{
int year,month,day;
public:
Date();// Default constructor
Date(int y,int m,int d);
int getYear();
int getMonth();
int getDay();
void setDate(int y,int m,int d);
void print();
void addOneDay();
};
Date::Date()
{
year=1900,month=1,day=1;
}
Date::Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
int Date::getDay()
{
return day;
}
int Date::getMonth()
{
return month;
}
int Date::getYear()
{
return year;
}
void Date::setDate(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
void Date::print()
{
cout<<setfill('0')<<setw(4)<<year<<'/'<<setw(2)<<month<<'/'<<setw(2)<<day<<endl;
}
void Date::addOneDay()
{
// Put the days of each month into the array
int mon[13]={
0,31,28,31,30,31,30,31,31,30,31,30,31};
int Leap;
if(year%4==0&&year%100!=0||year%400==0) Leap=1;
else Leap=0;
if(Leap) mon[2]=29;
day++;
if(day>mon[month])
{
month++;
day=1;
}
if(month>12)
{
year++;
month=1;
}
}
int main()
{
int t,ye,mo,da;
cin>>t;
Date p1;
while(t--)
{
cin>>ye>>mo>>da;
p1.setDate(ye,mo,da);
cout<<"Today is ";
p1.print() ;
p1.addOneDay();
cout<<"Tomorrow is ";
p1.print() ;
}
}
C. Fraction class ( Class and construction )
Title Description
Complete the implementation of the following score classes :
class CFraction
{
private:
int fz, fm;
public:
CFraction(int fz_val, int fm_val) ;
CFraction add(const CFraction &r);
CFraction sub(const CFraction &r);
CFraction mul(const CFraction &r);
CFraction div(const CFraction &r);
int getGCD(); // Find the maximum common divisor of the numerator and denominator of the object
void print();
};
Find two numbers a、b The greatest common divisor of can be divided by rolling , Also known as Euclid algorithm , The steps are :
- In exchange for a, b send a > b;
- use a except b Get the remainder r, if r=0, be b For the greatest common divisor , sign out .
- if r Not for 0, Then use b Instead of a, r Instead of b, here a,b It's smaller than the last one , The problem has shrunk ;
- Continue to the first 2 Step .
Input
Number of groups of test data t
The first score of the first group
The second score of the first group
The first score in the second group
The second score of the second group
…
Output
The sum of the two scores in the first group
The difference between the two scores in the first group
The product of the first set of two scores
The quotient of the first set of two scores
The sum of the two scores in the second group
The difference between the two scores in the second group
The second set is the product of two scores
The quotient of two scores in the second group
…
sample input
3
1/2
2/3
3/4
5/8
21/23
8/13
sample output
7/6
-1/6
1/3
3/4
11/8
1/8
15/32
6/5
457/299
89/299
168/299
273/184
Reference code
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
class CFraction
{
int fz, fm;// Molecular denominator
public:
CFraction(int fz_val, int fm_val) ;
CFraction add(const CFraction &r);// Add
CFraction sub(const CFraction &r);// reduce
CFraction mul(const CFraction &r);// ride
CFraction div(const CFraction &r);// except
int getGCD(); // Find the maximum common divisor of the numerator and denominator of the object
void print();
CFraction(){
};
};
CFraction::CFraction(int fz_val, int fm_val)
{
fz=fz_val;
fm=fm_val;
}
CFraction CFraction::add(const CFraction &r)
{
CFraction temp(fz*r.fm+r.fz *fm,fm*r.fm);
int gcd=temp.getGCD();
temp.fz=temp.fz/gcd;
temp.fm=temp.fm/gcd;
return temp;
}
CFraction CFraction::sub(const CFraction &r)
{
CFraction temp((fz*r.fm-r.fz *fm),fm*r.fm);
int gcd=temp.getGCD();
temp.fz=temp.fz/gcd;
temp.fm=temp.fm/gcd;
return temp;
}
CFraction CFraction::mul(const CFraction &r)
{
CFraction temp(fz*r.fz,fm*r.fm);
int gcd=temp.getGCD();
temp.fz=temp.fz/gcd;
temp.fm=temp.fm/gcd;
return temp;
}
CFraction CFraction::div(const CFraction &r)
{
CFraction temp(fz*r.fm,r.fz *fm);
int gcd=temp.getGCD();
temp.fz=temp.fz/gcd;
temp.fm=temp.fm/gcd;
return temp;
}
int CFraction::getGCD()
{
int r;
int a,b;
if(fabs(fz)>fabs(fm))
{
a=fabs(fz);
b=fabs(fm);
}
else
{
a=fabs(fm);
b=fabs(fz);
}
while(a%b!=0)// Find the greatest common divisor by the division of rolling
{
r=a%b;
a=b;
b=r;
}
return b;
}
void CFraction::print()
{
// If there are negative numbers , Notice that the minus sign comes first
if(fz<0&&fm>0||fz>0&&fm<0) cout<<'-';
cout<<fabs(fz)<<'/'<<fabs(fm)<<endl;
}
int main()
{
int t;
cin>>t;
int fz1,fm1,fz2,fm2;
CFraction p3;
while(t--)
{
char ch;
cin>>fz1>>ch>>fm1>>fz2>>ch>>fm2;
CFraction p1(fz1,fm1);
CFraction p2(fz2,fm2);
p3=p1.add(p2);// To add
p3.print();
p3=p1.sub(p2); // Subtraction
p3.print();
p3=p1.mul(p2);// Multiplication
p3.print();
p3=p1.div(p2);// division
p3.print();
cout<<endl;
}
}
D. Point_Array( class + structure + An array of objects )
Title Description

The above is an exercise we have practiced , Please make the following modifications on the basis of the original code :1、 Add self written destructor ;2、 take getDisTo The parameter of the method is modified to getDisTo(const Point &p);3、 Modify the corresponding constructor according to the output below .
Then, in the main function, it is established according to the number of user inputs Point Array , Find the distance between the two points with the largest distance in the array .
Input
Number of groups of test data t
Number of points in the first group
The first point is x coordinate y coordinate
On the second point x coordinate y coordinate
…
Output
Output the two points with the largest distance in the first group and their distance
…
stay C++ in , The reference code for outputting the specified precision is as follows :
#include
#include // Must contain this header file
using namespace std;
void main( )
{ double a =3.141596;
cout<<fixed<<setprecision(3)<<a<<endl; // Output after decimal point 3 position
sample input
2
4
0 0
5 0
5 5
2 10
3
-1 -8
0 9
5 0
sample output
Constructor.
Constructor.
Constructor.
Constructor.
The longeset distance is 10.44,between p[1] and p[3].
Distructor.
Distructor.
Distructor.
Distructor.
Constructor.
Constructor.
Constructor.
The longeset distance is 17.03,between p[0] and p[1].
Distructor.
Distructor.
Distructor.
Reference code
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
class Point
{
double x,y;
public:
Point();// Default constructor
~Point()
{
cout<<"Distructor."<<endl;
}
Point(double x_value,double y_value);// There are parameter constructors
double getX();// return x Value
double getY();// return y Value
void setXY(double x1,double y1)// Set the... In the array x,y
{
x=x1;
y=y1;
}
void setX(double x_value);
void setY(double y_value);
double getDisTo(const Point &p);
};
Point::Point()
{
cout<<"Constructor."<<endl;
x=0;
y=0;
}
Point::Point(double x_value,double y_value)// There are parameter constructors
{
x=x_value;
y=y_value;
}
double Point::getX()// return x Value
{
return x;
}
double Point::getY()// return y Value
{
return y;
}
void Point::setX(double x_value)// Set up x
{
x=x_value;
}
void Point::setY(double y_value)// Set up y
{
y=y_value;
}
double Point::getDisTo(const Point &p)
{
double len_x;
double len_y;
double len;
len_x=x-p.x;
len_y=y-p.y;
len=sqrt(len_x*len_x+len_y*len_y);
return len;
}
int main()
{
int t;// Number of test groups
int n;// Number of points
double x,y,dis;
cin>>t;
while(t--)
{
cin>>n;
Point *p=new Point[n];
for(int i=0;i<n;i++)
{
cin>>x>>y;
p[i].setXY(x,y);
}
double max=-99999;
int tag1,tag2;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(p[i].getDisTo(p[j])>max)
{
max=p[i].getDisTo(p[j]);
tag1=i;
tag2=j;// Record which two points generate the maximum value
}
}
}
if(tag1>tag2)
{
int tem;
tem=tag1;
tag1=tag2;
tag2=tem;
}
cout<<"The longeset distance is "<<fixed<<setprecision(2)<<max<<",between p["<<tag1<<"] and p["<<tag2<<"]."<<endl;
delete []p;
}
}
E. Point circle operation ( Structure and destruction )
Title Description
Design a point class Point, Contains private properties x Coordinates and y coordinate , Operations include
1、 Constructors , Two conditions are required :1. Can use class Point To create an array of objects ( Default constructor !);2. Capable of receiving external input x And coordinate initialization , Tips : Constructor overload
2、 Destructor , hold x Coordinates and y Coordinates are clear 0, And output information “point clear”
3、 Set up (setXY), Accept external input , And set up x Coordinates and y coordinate
4、 obtain x coordinate , Go straight back to x value
5、 obtain y coordinate , Go straight back to y value
Design a circle class Circle, Contains private properties : The coordinates of the center of the circle x and y、 radius r; Operations include :
1、 Constructors , Accept external input , Set the center x coordinate 、y Coordinates and radius
2、 Destructor , Coordinate the center of the circle x and y And the radius are cleared , And the output "circle clear"
3、 contain (Contain), Judge whether a circle contains a point , Calculate the distance from the center of the circle to this point , And then compare it with the radius , Greater than does not include , Less than or equal to includes . Tips : Using a point object as a parameter does not necessarily conform to the output
Input
On the first line, enter the of a point x Coordinates and y coordinate , use Point Class to create a point object , And is automatically initialized by the constructor
Second line input n, use Point Class to create an array of point objects , contain n A little bit
Enter... From the third line n That's ok , Enter a dot per line x and y coordinate , Use settings (setXY) For each point x and y coordinate
Then enter three parameters on one line , Represents the coordinates of the center of a circle x and y, And the radius , Use Circle Class to create a circular object , And automatically initialize through the constructor
Output
By calling the inclusion of the circle (Contain) Method , Determine whether each point is in the circle .
According to the input order of points , Output the judgment result of one point per line , If included, output in, If not, output out
explain : When an array of objects is created dynamically , So at the end of the program , This array is not recycled . Only add code delete []p, To reclaim the array .
This question does not require the retrieval of arrays .
sample input
5 2
3
4 7
9 9
2 4
3 3 3
sample output
in
out
out
in
circle clear
point clear
Reference code
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
class Point
{
int x,y;
public:
Point()// Default constructor
{
x=0;
y=0;
}
~Point()
{
x=0;
y=0;
cout<<"point clear"<<endl;
}
Point(int x_value,int y_value)// There are parameter constructors
{
x=x_value;
y=y_value;
}
int getX()// return x Value
{
return x;
}
int getY()// return y Value
{
return y;
}
void setXY(int x1,int y1)// Set the... In the array x,y
{
x=x1;
y=y1;
}
};
class Circle
{
int x;int y;int r;
public:
Circle(int x1,int y1,int r1)
{
x=x1;
y=y1;
r=r1;
}
~Circle()
{
x=0;
y=0;
r=0;
cout<<"circle clear"<<endl;
}
void setXY(int x1,int y1,int r1)
{
x=x1;
y=y1;
r=r1;
}
void contain(int x1,int y1)
{
if(sqrt((x1-x)*(x1-x)+(y1-y)*(y1-y))<=r) cout<<"in"<<endl;
else cout<<"out"<<endl;
}
};
int main()
{
int n;// Number of points
int p1_x,p1_y,x,y;// The coordinates of point
int cir_x,cir_y,r;// The coordinates of the center of the circle , radius
cin>>p1_x>>p1_y;
Point p1(p1_x,p1_y);
cin>>n;
Point *p=new Point[n];
for(int i=0;i<n;i++)
{
cin>>x>>y;
p[i].setXY(x,y);
}
cin>>cir_x>>cir_y>>r;
Circle c(cir_x,cir_y,r);// initialization
c.contain(p1_x,p1_y);
for(int i=0;i<n;i++)
{
x=p[i].getX();
y=p[i].getY();
c.contain(x,y);
}
//delete []p;
}
边栏推荐
- UVM: transaction level modeling (TLM) 1.0 communication standard
- Balanced binary tree (AVL tree)
- Binary sort tree
- 月份选择器禁用当月以后的数据 包含当月
- Achievements in science and Technology (XV)
- 愉快无负担的跨进程通信方式
- SF14 | supertrend "super trend" indicator magic change and upgrade (source code)
- [flume] notes
- 手机wps如何压缩文件打包发送
- Gin integrated graphic verification code
猜你喜欢

MySQL basic tutorial -- MySQL transaction and storage engine

(linear DP) acwing 898 Number triangle

Do you want to take the postgraduate entrance examination? Will you be able to find a good job after graduate school?

C language exercise: esp32 ble Low Power Bluetooth server data packaging and client data analysis

Ar helps brand stores achieve global data growth

Jenkins basic configuration
![将数组分成和相等的三个部分[问题分析]](/img/0b/856fcceb0373baa8acb46e9ae2c861.png)
将数组分成和相等的三个部分[问题分析]

(greedy + longest ascending subsequence) acwing 896 Longest ascending subsequence II

On the knowledge points of cookie attributes and the differences between webstorage and cookies?

The road of global evolution of vivo global mall -- multilingual solution
随机推荐
MySQL some simple commands
Summary of DOM knowledge points
[day 5 of JUC learning] reference atomic classes and attribute modifiers
wps表格怎么取消智能表格样式
Jenkins基本配置
What is bom? And knowledge points
IP addressing overview
干货|一次完整的性能测试,测试人员需要做什么?
预解析与作用域
chisel环境搭建(win10 + vscode)
730.Count Different Palindromic Subsequences
(counting class +dp) acwing 900 Integer partition
(interval DP | dfs+ memory) acwing 282 Stone merging
(dp+ group backpack) acwing 9 Group knapsack problem
SAP SD create / modify price list
Shell (32): configure SSH privacy free
C language leetcode deleting duplicate items in an ordered array
Read 5g RF terminal industry
(dp) acwing 899. Edit distance
Introduction and installation steps of sonarqube