1.創(chuàng)建類,屬性,行為。
#includeusing namespace std;
const double PI = 3.14;
//設(shè)計(jì)一個(gè)圓類,求圓的周長(zhǎng)
class Circle
{//訪問權(quán)限
//公共權(quán)限
public:
//屬性
int m_r;//半徑
//行為
double calculateC()//求周長(zhǎng)
{return 2 * PI * m_r;
}
};
//設(shè)計(jì)一個(gè)學(xué)生類
class Student
{//訪問權(quán)限
//公共權(quán)限
public:
//屬性
string name;//姓名
int id;//學(xué)號(hào)
//行為
void showStudent()//展示學(xué)生姓名和學(xué)號(hào)
{cout<< "姓名:"<< name<< "學(xué)號(hào):"<< id<< endl;
}
};
int main()
{//通過圓類,創(chuàng)建具體的圓(對(duì)象)
//實(shí)例化
Circle c1;
c1.m_r = 10;
cout<< "圓的周長(zhǎng)"<< c1.calculateC()<< endl;
//實(shí)例化學(xué)生對(duì)象
Student s1;
s1.name = "蔡徐坤";
s1.id = 1104;
Student s2;
s2.name = "趙四";
s2.id = 1105;
s1.showStudent();
s2.showStudent();
system("pause");
return 0;
}
結(jié)果
圓的周長(zhǎng)62.8
姓名:蔡徐坤學(xué)號(hào):1104
姓名:趙四學(xué)號(hào):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 = "保時(shí)捷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.對(duì)于寫,可檢測(cè)數(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ù)
//::告訴是哪個(gè)作用域下的成員函數(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;//半徑
//在類中可以讓另一個(gè)類 作為本來中的成員
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)
{//計(jì)算點(diǎn)到圓心的距離
int distance =
(c.getCenter().getX() - p.getX())* (c.getCenter().getX() - p.getX()) +
(c.getCenter().getY() - p.getY())* (c.getCenter().getY() - p.getY());
//計(jì)算半徑的平方
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級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)頁(yè)名稱:有C基礎(chǔ)學(xué)習(xí)C++第八天(類和對(duì)象-封裝)-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)地址:http://aaarwkj.com/article0/dsjhio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、Google、外貿(mào)網(wǎng)站建設(shè)、面包屑導(dǎo)航、定制開發(fā)、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容