栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++学习

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++学习

struct 和 class 的区别: 

#include
using namespace std;
//struct 和 class 的区别:
//struct 默认权限为:公共
//class 默认权限为: 私有
class C1
{
	int m_A;	
};//private 
struct C2
{
	int m_A;	
};//public 
int main()
{
	C1 c1;
//	c1.m_A = 100;
	//私有属性不可以在类外访问 
	C2 c2;
	c2.m_A = 100;
	//公共属性可以在类外访问 
	return 0;
}
//将成员属性设置为私有
#include
using namespace std;
//#include

class Person{
	public:
	void setName(string name){
		m_Name = name;
	}
	string getName(){
		return m_Name;
	}
	void setAge(int Age){
		if(Age<0||Age>150){
			m_Age = 0;
			cout<<"Error!!!"< 

设计一个立方体类:

//设计一个立方体类
//设计立方体类(Cube)
//求出立方体的面积和体积
//分别使用全局函数 和 成员函数 判断两个立方体是否相等
#include
//全局函数传入两个参数 成员函数传入一个参数
 
using namespace std;

class Cube{
	private:
		int m_L;//长 
		int m_H;//高 
		int m_W;//宽 
	public:
		//通过成员函数来判断
		bool isSameCube(Cube &c){
			if(c.getL()==m_L&&c.getW()==m_W&&c.getH()==m_H){
				cout<<"True"< 

设计一个圆与点类:

//点和圆的关系
//设计一个圆形类(Circle) 一个点类(Point) 计算点和圆的关系
#include
using namespace std;
#include "Circle.h"
#include "point.h" 
//核心为: 在一个类(Circle)当中 可以让另一个类(Point) 作为本类当中的核心成员
 
//class Point{
//	public:
//		void setX(int x){
//			m_X = x;
//		}
//		int getX(){
//			return m_X;
//		}
//		void setY(int y){
//			m_Y = y;
//		}
//		int getY(){
//			return m_Y;
//		} 
//	private:
//		int m_X;
//		int m_Y;
//}; 
//点到圆心的距离:== 半径 点在圆上
//点到圆心的距离:<  半径 点在圆内
//点到圆心的距离:>  半径 点在圆外
//class Circle{
//	public:
//		void setR(int r){
//			m_R = r;
//		}
//		int getR(){
//			return m_R;
//		}
//		void setCenter(Point c){
//			m_Center = c;
//		}
//		Point getPoint(){
//			return m_Center;
//		}
//	private:
//		int m_R;
//		Point m_Center;
//};
//求两点之间的坐标
//求两点之间的距离公式 
//(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) 最后整体开根号
 
//全局函数:
void isInCircle(Circle &c,Point &p){
	//首先传入一个圆 和 一个点
	 //求两点之间的距离
	int sum = 0;
	int num = 0;  
	sum = (c.getPoint().getX()-p.getX())*(c.getPoint().getX()-p.getX())+(c.getPoint().getY()-p.getY())*(c.getPoint().getY()-p.getY());
	  
	//两点之间的距离平方
	num = c.getR()*c.getR();
	//半径R的平方
	if(sum==num){
		cout<<"相等"<<" "<<"在圆上"<num){
		cout<<"不相等"<<" "<<"在圆外"< 

可以将其分成项目:

point.h
#pragma once
//防止头文件重复包含
#include
using namespace std;
class Point{
	public:
		void setX(int x);
		int getX();
		void setY(int y);
		int getY();
	private:
		int m_X;
		int m_Y;
}; 
//保留函数的声明和变量的声明
 
point.cpp
#include "point.h"
void Point::setX(int x){
	m_X = x;
}
int Point::getX(){
	return m_X;
}
void Point::setY(int y){
	m_Y = y;
}
int Point::getY(){
	return m_Y;
}
//shift + tab 缩进
 
Circle.h
#pragma once
#include
using namespace std;
#include "point.h"
class Circle{
	public:
		void setR(int r);
		int getR();
		void setCenter(Point c);
		Point getPoint();
	private:
		int m_R;
		Point m_Center;
};
Circle.cpp
#include "Circle.h"
void Circle::setR(int r){
	m_R = r;
}
int Circle::getR(){
	return m_R;
}
void Circle::setCenter(Point c){
	m_Center = c;
}
Point Circle::getPoint(){
	return m_Center;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1008573.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号