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

C++学习笔记(Day7 第五章 数据的共享与保护——类的静态数据成员、静态函数成员 类的友元 共享数据的保护)

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

C++学习笔记(Day7 第五章 数据的共享与保护——类的静态数据成员、静态函数成员 类的友元 共享数据的保护)

疑问:

Point(int x = 0, int y = 0) : x(x), y(y){
}//通过构造函数的初始化列表给对象的常数据成员赋初值
  1. x(x),y(y)是什么意思?
    通过构造函数的初始化列表给对象的常数据成员赋初值
  2. 为什么静态函数成员只能访问静态数据成员?
  3. 值传递 vs 引用传递(双向)
  4. 哪些操作会有试图改变常对象状态的危险?
    采用常引用friend float dist(const Point &p1,const Point &p2);
  5. 静态数据成员在类内声明,类外进行初始化并使用类名限定
  6. 常数据成员可以在定义时直接初始化(C++11)

例5-4

//5_4.cpp
#include 
using namespace std;
class Point { //Point类定义
public: //外部接口
    Point(int x = 0, int y = 0) : x(x), y(y) { //构造函数
//在构造函数中对count累加,所有对象共同维护同一个count
        count++;
    }
    Point(Point &p) { //复制构造函数
        x = p.x;
        y = p.y;
        count++;
    }
    ~Point() { count--; }
    int getX() { return x; }
    int getY() { return y; }
    void showCount() { //输出静态数据成员
        cout << " Object count = " << count << endl;
    }
private: //私有数据成员
    int x, y;
    static int count; //静态数据成员声明,用于记录点的个数
};
int Point::count = 0;//静态数据成员定义和初始化,使用类名限定
int main() { //主函数
    Point a(4, 5); //定义对象a,其构造函数会使count增1
    cout << "Point A: " << a.getX() << ", " << a.getY();
    a.showCount(); //输出对象个数
    Point b(a); //定义对象b,其构造函数会使count增1
    cout << "Point B: " << b.getX() << ", " << b.getY();
    b.showCount(); //输出对象个数
    return 0;
}

例 5-5 具有静态数据、函数成员的 Point 类

#include 
using namespace std;
class Point {
public:
    Point(int x = 0, int y = 0) : x(x), y(y) { count++; }//构造函数
    Point(Point &p) { //复制构造函数
        x = p.x;
        y = p.y;
        count++;
    }
    ~Point() { count--; }
    int getX() { return x; }
    int getY() { return y; }
    static void showCount() {
        cout << "Object count = " << count << endl;
    }
private:
    int x, y;
    static int count; //静态数据成员声明,用于记录点的个数
};
int Point::count = 0;//静态数据成员定义和初始化,使用类名限定
int main() {
    Point a(4, 5); //定义对象a,其构造函数会使count增1
    cout << "Point A: " << a.getX() << ", " << a.getY() << endl;
    a.showCount(); //输出对象个数
    //Point::showCount();是一样的
    Point b(a); //定义对象b,其构造函数会使count增1
    cout << "Point B: " << b.getX() << ", " << b.getY() << endl;
    b.showCount(); //输出对象个数
    return 0;
}

类的友元

封装隐藏(安全性)和效率(快速性)中找平衡、

共享数据的保护 常对象
#include
using namespace std;
class A
{
public:
    A(int i,int j) {
        x=i; y=j;}
private:
    int x,y;
};
int main(){
    A const a(3,4);//a是常对象,不能被更新
}
常成员函数
#include
using namespace std;
class R {
public:
    R(int r1, int r2) : r1(r1), r2(r2) { }
    void print();
    void print() const;//区分重载函数
private:
    int r1, r2;
};
void R::print() {
    cout << r1 << ":" << r2 << endl;
}
void R::print() const {
    cout << r1 << ";" << r2 << endl;
}
int main() {
    R a(5,4);
    a.print(); //调用void print()
    const R b(20,52);
    b.print(); //调用void print() const
    return 0;
}
常数据成员
#include 
using namespace std;
class A {
public:
    A(int i);
    void print();
private:
    const int a;
    static const int b; //静态常数据成员
    //在类体内声明
};
const int A::b=10;//在类体外初始化
A::A(int i) : a(i) { }//通过构造函数的初始化列表给对象的常数据成员赋初值
void A::print() {
    cout << a << ":" << b < 
常引用 

例题 常引用做形参

#include 
#include 
using namespace std;
class Point { //Point类定义
public: //外部接口
Point(int x = 0, int y = 0)
 : x(x), y(y) { }
int getX() { return x; }
int getY() { return y; }
friend float dist(const Point &p1,const Point &p2);
private: //私有数据成员
int x, y;
};
float dist(const Point &p1, const Point &p2) {
double x = p1.x - p2.x;
double y = p1.y - p2.y;
return static_cast(sqrt(x*x+y*y));
}
int main() { //主函数
const Point myp1(1, 1), myp2(4, 5);
cout << "The distance is: ";
cout << dist(myp1, myp2) << endl;
return 0;
}

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

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

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