#include <iostream>
using namespace std;
#include <string>
//点和圆的关系
//点和圆的关系
//设置点类
class point
{
private:
int m_X;
int m_Y;
public:
//设置X坐标
void set_X(int X)
{
m_X = X;
}
//获取X坐标
int get_X()
{
return m_X;
}
//设置Y坐标
void set_Y(int Y)
{
m_Y = Y;
}
//获取Y坐标
int get_Y()
{
return m_Y;
}
};
class circle
{
private:
int m_R;
point C0;//圆心
public:
//设置半径
void set_R(int R)
{
m_R = R;
}
//获取半径
int get_R()
{
return m_R;
}
//设置圆心
void set_C(point C)
{
C0 = C;
}
//获取圆心
point get_C()
{
return C0;
}
};
//判断点和圆的关系
void point_circle(point A, circle B)
{
int a = A.get_X() - B.get_C().get_X();
int b = A.get_Y() - B.get_C().get_Y();
if (a *a + b *b == B.get_R()* B.get_R() )
{
cout << "在圆上" << endl;
}
else if (a * a + b * b > B.get_R() * B.get_R())
{
cout << "在圆外" << endl;
}
else
{
cout << "在圆内" << endl;
}
}
int main()
{
//设置点
point A;
A.set_X(0);
A.set_Y(10);
//设置圆
point C;
C.set_X(10);
C.set_Y(10);
circle B;
B.set_C(C);
B.set_R(20);
point_circle(A, B);
}
//代码中类都是分开编写的,在使用时开头加上 #include "xxx.h"
//如何分开 类
//生成头文件(只要变量和函数声明),开头多一行#pragma once ,后面是#include。。。。
//生成源文件(函数表达) ,只有开头一行#include "xx.h" ,函数前加 类名::说明作用域
point 头文件
#pragma once
#include <iostream>
using namespace std;
class point
{ ///只保留声明即可
private:
int m_X;
int m_Y;
public:
//设置X坐标
void set_X(int X);
//获取X坐标
int get_X();
//设置Y坐标
void set_Y(int Y);
//获取Y坐标
int get_Y();
};
point 源文件
#include "point.h"
//设置X坐标
void point::set_X(int X)///函数前写 point:: 类名称
{
m_X = X;
}
//获取X坐标
int point::get_X()
{
return m_X;
}
//设置Y坐标
void point::set_Y(int Y)
{
m_Y = Y;
}
//获取Y坐标
int point::get_Y()
{
return m_Y;
}
circle 头文件
#pragma once
#include <iostream>
using namespace std;
#include "point.h" circle类中又用到point这个类,则需要声明一下
class circle
{
private:
int m_R;
point C0;//圆心
public:
//设置半径
void set_R(int R);
//获取半径
int get_R();
//设置圆心
void set_C(point C);
//获取圆心
point get_C();
};
/ circle 源文件
#include "circle.h"
//设置半径
void circle::set_R(int R)
{
m_R = R;
}
//获取半径
int circle::get_R()
{
return m_R;
}
//设置圆心
void circle::set_C(point C)
{
C0 = C;
}
//获取圆心
point circle::get_C()
{
return C0;
}
/初始化 和 清理,两个函数是与类名相同
//构造函数 类名(){} 可以有参数,可重载
//析构函数 ~类名(){} 不可有参数,不可重载
//析构函数是在 对象 消失的时候才运行,比如对象在栈区
#include <iostream>
using namespace std;
#include <string>
// 对象的初始化 清理
//构造函数 初始化
class person
{
public:
person()//构造
{
cout << "person 构造函数" << endl;
}
~person()//析构
{
cout << "person 析构函数" << endl;
}
};
void test()
{
person p;
}
int main
{
test();
person p;
}
#include <iostream>
using namespace std;
#include <string>
//构造函数的分类
//有参 无参
//普通 拷贝
class person
{
public:
person()// 无参 默认
{
cout << "构造函数" << endl;
}
person(int a)//有参 定义有参构造后就没有无参构造了
{
age = a;
cout << "有参构造函数" << endl;
}
person(const person &p)//拷贝 将传入的数据做拷贝处理
{
cout << "拷贝构造函数" << endl;
age = p.age;//拷贝 将传入的数据做拷贝处理
}
~person()
{
cout << "析构函数" << endl;
}
int age;
};
void test()
{
person p;
}
void test01()
{
//构造函数调用
//括号调用/最常用
person p;//调用无参构造不要带()
person p1(10);
person p2(p1);
//显示调用
person p3 = person(10);
person p4 = person(p3);
person(10);//当前行执行完后就立即收回
person(p1);// 不是拷贝构造 ,person(p1)=person p1
//隐式转换
person p5 = 10;//有参
person p6 = p5;//拷贝
}
int main()
{
test01();
}
//拷贝构造函数调用时机
#include <iostream>
using namespace std;
#include <string>
//拷贝构造函数调用时机
class person
{
public:
person()
{
cout << "无参构造" << endl;
}
person(int a)
{
m_age = a;
cout << "有参构造" << endl;
}
person(const person& p)
{
m_age = p.m_age;
cout << "拷贝构造" << endl;
}
~person()
{
cout << "析构函数" << endl;
}
int m_age;
};
// 用已经创建完的对象来初始化新对象
void test()
{
person p1(20);
person p2(p1);//拷贝
cout << p2.m_age << endl;
}
//值传递方式给函数参数传值
void dowork(person p)
{
}
void test01()
{
person p3;
dowork(p3);//拷贝
}
//值方式返回局部对象
person dowork01()
{
person p4;
return p4;
}
void test03()
{
person p5 = dowork01();//拷贝
}
int main()
{
test();
test01();//值传递
test03();//值方式返回局部对象
}
//一个类 默认有三个函数
//构造 析构 拷贝
//定义有参构造,就没有无参 ; 拷贝还存在
//定义拷贝 ; 其他普通构造就都没有
// 自己定义的高级构造函数 会自动屏蔽编译器提供的构造函数
C++学习路程 22/3/8 PM 14:52
最新推荐文章于 2024-06-15 16:46:40 发布