有C基礎(chǔ)學(xué)習(xí)C++第八天(類和對象-封裝)-創(chuàng)新互聯(lián)

1.創(chuàng)建類,屬性,行為。

十載的東河網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整東河建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)建站從事“東河網(wǎng)站設(shè)計”,“東河網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實(shí)執(zhí)行。
#includeusing namespace std;

const double PI = 3.14;

//設(shè)計一個圓類,求圓的周長
class Circle
{//訪問權(quán)限
	//公共權(quán)限
public:
	//屬性
	int m_r;//半徑

	//行為
	double calculateC()//求周長
	{return 2 * PI * m_r;
	}
};

//設(shè)計一個學(xué)生類
class Student
{//訪問權(quán)限
	//公共權(quán)限
public:
	//屬性
	string name;//姓名
	int id;//學(xué)號
	//行為
	void showStudent()//展示學(xué)生姓名和學(xué)號
	{cout<< "姓名:"<< name<< "學(xué)號:"<< id<< endl;
	}
};

int main()
{//通過圓類,創(chuàng)建具體的圓(對象)
	//實(shí)例化
	Circle c1;
	c1.m_r = 10;
	cout<< "圓的周長"<< c1.calculateC()<< endl;

	//實(shí)例化學(xué)生對象
	Student s1;
	s1.name = "蔡徐坤";
	s1.id = 1104;
	Student s2;
	s2.name = "趙四";
	s2.id = 1105;
	s1.showStudent();
	s2.showStudent();


	system("pause");
	return 0;
}

結(jié)果

圓的周長62.8
姓名:蔡徐坤學(xué)號:1104
姓名:趙四學(xué)號:1105

2.3種訪問權(quán)限,公共權(quán)限 public ,保護(hù)權(quán)限 protected,私有權(quán)限 private。

#includeusing namespace std;

//3種訪問權(quán)限
//公共權(quán)限 public     //成員類內(nèi)可以訪問 類外可以訪問
//保護(hù)權(quán)限 protected  //成員類內(nèi)可以訪問 類外不可以訪問
//私有權(quán)限 private    //成員類內(nèi)可以訪問 類外不可以訪問

class Person
{public:
	string name;

protected:
	string car;

private:
	int password;

//類內(nèi)
public:
	void func()
	{name = "蔡徐坤";
		car = "保時捷911";
		password = 123456;
	}
};


int main()
{Person p1;
	//類外
	p1.name = "張三";//成功
	//p1.car = "本田";//訪問失敗
	//p1.password = 225344;//訪問失敗


	system("pause");
	return 0;
}

3.class和struct 的區(qū)別

#includeusing namespace std;

//class和struct 的區(qū)別

class C1
{int a;//默認(rèn)權(quán)限 是私有
};
struct C2
{int a;//默認(rèn)權(quán)限 是公共
};


int main()
{C1 c1;
	C2 c2;
	//c1.a = 100;//訪問失敗
	c2.a = 100;//成功


	system("pause");
	return 0;
}

4.成員屬性私有化。

#includeusing namespace std;

//成員屬性設(shè)置為私有
//1.可以自己控制讀寫權(quán)限
//2.對于寫,可檢測數(shù)據(jù)的有效性

class Person
{public:
	void setName(string a)//姓名寫
	{name = a;
	}
	string getName()//姓名讀
	{return name;
	}
	int getAge()//年齡讀
	{return age;
	}
	string setSex(string b)//性別寫
	{sex = b;
	}
private:
	//姓名 可讀可寫
	string name;
	//年齡 只讀
	int age = 15;
	//性別 只寫
	string sex;
};


int main()
{Person p;
	p.setName("蔡徐坤");
	cout<< "姓名為:"<< p.getName()<< endl;

	cout<< "年齡為:"<< p.getAge()<< endl;

	p.setSex("男");

	system("pause");
	return 0;
}
姓名為:蔡徐坤
年齡為:15

5.點(diǎn)和圓關(guān)系案例。

501創(chuàng)建點(diǎn)類
point.h

#pragma once
#includeusing namespace std;

//點(diǎn)類
//聲明和初始化
class Point
{public:
	//設(shè)置x坐標(biāo)
	void setX(int x);
	
	//讀取x坐標(biāo)
	int getX();
	
	//設(shè)置y坐標(biāo)
	void setY(int y);
	
	//讀取y坐標(biāo)
	int getY();
	

private:
	int X;//x坐標(biāo)
	int Y;//y坐標(biāo)
};

point.cpp

#include "point.h"

//放置函數(shù)
//::告訴是哪個作用域下的成員函數(shù)
//設(shè)置x坐標(biāo)
void Point::setX(int x)
{X = x;
}
//讀取x坐標(biāo)
int Point::getX()
{return X;
}
//設(shè)置y坐標(biāo)
void Point::setY(int y)
{Y = y;
}
//讀取y坐標(biāo)
int Point::getY()
{return Y;
}

502創(chuàng)建圓類
circle.h

#pragma once
#include#include "point.h"
using namespace std;

//圓類
//聲明和初始化
class Circle
{public:
	//設(shè)置半徑
	void setr(int a);
	
	//獲取半徑
	int getr();
	
	//設(shè)置圓心 
	void setCenter(Point b);
	
	//獲取圓心
	Point getCenter();
	

private:
	int r;//半徑
	//在類中可以讓另一個類 作為本來中的成員
	Point center;//圓心
};

circle.cpp

#include "circle.h"

//圓類

//設(shè)置半徑
void Circle::setr(int a)
{r = a;
}
//獲取半徑
int Circle::getr()
{return r;
}
//設(shè)置圓心 
void Circle::setCenter(Point b)
{center = b;
}
//獲取圓心
Point Circle::getCenter()
{return center;
}

503主函數(shù),主文件

#include#include "circle.h";
#include "point.h"
using namespace std;

//點(diǎn)和圓關(guān)系案例


//判斷點(diǎn)和圓關(guān)系
void isInCircle(Circle& c, Point& p)
{//計算點(diǎn)到圓心的距離
	int distance =
	(c.getCenter().getX() - p.getX())* (c.getCenter().getX() - p.getX()) + 
	(c.getCenter().getY() - p.getY())* (c.getCenter().getY() - p.getY());
	//計算半徑的平方
	int RchengR = c.getr() * c.getr();

	//判斷關(guān)系
	if (distance == RchengR)
	{cout<< "點(diǎn)在圓上"<< endl;
	}
	if (distance< RchengR)
	{cout<< "點(diǎn)在圓內(nèi)"<< endl;
	}
	if (distance >RchengR)
	{cout<< "點(diǎn)在圓外"<< endl;
	}
}


int main()
{//創(chuàng)建圓
	Circle c;
	c.setr(10);
		//創(chuàng)建圓的圓心
	Point center;
	center.setX(10);
	center.setY(0);
	c.setCenter(center);

	//創(chuàng)建點(diǎn)
	Point p1;
	p1.setX(10);
	p1.setY(10);
	Point p2;
	p2.setX(10);
	p2.setY(9);
	Point p3;
	p3.setX(18);
	p3.setY(18);

	//判斷關(guān)系
	isInCircle(c, p1);
	isInCircle(c, p2);
	isInCircle(c, p3);


	system("pause");
	return 0;
}

結(jié)果

點(diǎn)在圓上
點(diǎn)在圓內(nèi)
點(diǎn)在圓外

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)頁名稱:有C基礎(chǔ)學(xué)習(xí)C++第八天(類和對象-封裝)-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://muchs.cn/article0/dsjhio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、Google、外貿(mào)網(wǎng)站建設(shè)、面包屑導(dǎo)航定制開發(fā)、品牌網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)