嵌套类

Club嵌套Coach

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Coach{
string name;
int winRate;
public:
Coach(string n, int wr){
name=n; winRate=wr;
}
void show();
};
class Club{
string name;
Coach c;
int year;
public:
Club(string n1, int y, string n2, int wr);
void show();
};

Club的初始化和show函数要利用Club类中的东西

1
2
3
4
5
6
7
8
Club::Club(string n1,int y,string n2,int wr):c(n2,wr){
name = n1;
year = y;
}
void Club::show(){
cout << name << " " << year << "\n";
c.show();
}

同样,在下面Circle调用内部Point类型的变量,要有Point中的get函数配合才能取出相应的x,y

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<bits/stdc++.h>
using namespace std;
class Point{
private:
double x,y;
public:
Point(int n,int m):x(n),y(m){}
double get_x(){return x;}
double get_y(){return y;}
};
class Circle{
private:
Point o;
double r;
public:
Circle(Point p,double x):o(p){r = x;}
bool isPointIn(Point p){
double a = o.get_x();
double b = o.get_y();
double c = p.get_x();
double d = p.get_y();
double x = sqrt((a - c) * (a - c) + (b - d) * (b - d));
if(x < r)return true;
return false;
}
};