C++复习 -- 类的实现

C++ 类设计和实现的十大最佳实践 较高的LCOM值通常可以意味着类的内聚性较差。这些公式背后的基本思想可以表述如下: 如果一个类的所有方法都使用它的所有实例字段,那么这个类就是完全内聚的,这意味着sum(MF)=M*F,然后LCOM = 0和LCOMHS = 0。Liskov的行为子类型的概念定义了可变对象可替换性的概念,也就是说,如果S是T的子类型,那么程序中T类型的对象可以被替换为S类型的对象,而不改变该程序的任何期望属性(例如,正确性)。代码库中充斥着嘈杂的注释和不正确的注释,促使程序员忽略所有的注释,或者采取积极的措施隐藏它们。 阅读详情

类:

概念

C++ 中的类(class)是一种编程结构,用于创建对象。

这些对象可以拥有属性(即数据成员)和行为(即成员函数或方法)。类的概念是面向对象编程的核心之一,其主要目的是将数据和与数据相关的操作封装在一起。例如,如果你有一个“汽车”类,它可能包含颜色、品牌、型号等属性(数据成员),以及启动、停止、加速等行为(成员函数)。每当你基于这个类创建一个对象时,你就有了一个具体的汽车,具有这些属性和行为。


C++ 类的基本结构:


1. 数据成员(Attributes):定义类的属性。这些是类内部的变量,用于存储对象的状态。
2. 成员函数(Methods):定义类的行为。这些是可以操作对象的数据成员的函数。
3. 构造函数和析构函数:特殊的成员函数。构造函数在创建对象时自动调用,用于初始化对象。析构函数在对象销毁时调用,用于执行清理操作。
4. 访问修饰符:如 public , private , protected ,用于控制对类成员的访问权限。例如, public成员可以在类的外部访问,而 private 成员只能在类内部访问。
5. 继承:允许一个类继承另一个类的特性。这是代码重用和多态性的关键。
通过这些特性,C++ 类提供了一种强大的方式来组织和处理数据,使得代码更加模块化、易于理解和维护。


===============================================
结构体 -> 结构体变量 

类 -> 对象

二者都是抽象 转变为具象


==========================

通过结构体引入类

更详细的内容请看这篇: 


C语言 + 结构体实现:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;


struct Car{ //汽车“类”
char *color; //颜色
char *brand; //品牌
char *type; //车型
int year; //年限
void (*printCarInfo)(char *color,char *brand,char *type, int year); //函数指针,指向车介绍函数
void (*carRun)(char *type); //函数指针,指向车运行的函数
void (*carStop)(char *type); //函数指针,执行车停止的函数
};

void bwmThreePrintCarInfo(char *color,char *brand,char *type, int year)
{
printf("车的品牌是:%s, 型号是: %s, 颜色是:%s,上市年限是%d\n",brand,type,color,year);
}

void A6PrintCarInfo(char *color,char *brand,char *type, int year)
{
printf("车的品牌是:%s,型号是: %s, 颜色是:%s, 上市年限是%d\n",brand,type,color,year);
}

int main()
{
    struct Car BWMthree;
    BWMthree.color = "白色";
    BWMthree.brand = "宝马";
    BWMthree.type = "3系";
    BWMthree.year = 2023;
    BWMthree.printCarInfo = bwmThreePrintCarInfo; // 给结构体里面的函数指针对象传递函数
    BWMthree.printCarInfo(BWMthree.color,BWMthree.brand,BWMthree.type,BWMthree.year);


    struct Car *AodiA6;// 不能直接 调用指针,要先申请内存空间不然,会野指针,爆段错误
    AodiA6 = (struct Car*)malloc(sizeof(struct Car));
    AodiA6->color = "黑色";
    AodiA6->brand = "奥迪";
    AodiA6->type = "A6";
    AodiA6->year = 2008;

    AodiA6->printCarInfo = A6PrintCarInfo;
    AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type,AodiA6->year);

    return 0;
}


=========================================


与C++实现的区别:


C++ 和C  语言的结构体能混着用,无非 就是常量字符串,C语言 的类型是 char * ,而C++是 string
C ++ 里面不用 malloc 去申请空间实例化指针,直接把struct 改为class,为类创建对象,调用new函数来实例化
注意: class  类里面的数据不写的情况下是私有权限 -->private -- 不能在main里面调用要声明为 public权限


C++ 的 面向对象的特征更加明显 --> 封装,继承,多态 (权限控制)

C++实现

修改如下:

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
class Car{ //汽车“类”
public:
string color; //颜色
string brand; //品牌
string type; //车型
int year; //年限
void (*printCarInfo)(string color,string brand,string type, int year); //函数指针,指向车介绍函数
void (*carRun)(string type); //函数指针,指向车运行的函数
void (*carStop)(string type); //函数指针,执行车停止的函数
};
void bwmThreePrintCarInfo(string color,string brand,string type, int year)
{
string str = "车的品牌是:" + brand
+ ",型号是: " + type
+ ",颜色是:" + color
+ ",上市年限是:" + std::to_string(year);
cout << str << endl;
}
void A6PrintCarInfo(string color,string brand,string type, int year)
{
string str = "车的品牌是:" + brand
+ ",型号是: " + type
+ ",颜色是:" + color
+ ",上市年限是:" + std::to_string(year);
cout << str << endl;
}
int main()
{
Car BWMthree;
BWMthree.color = "白色";
BWMthree.brand = "宝马";
BWMthree.type = "3系";
BWMthree.year = 2023;
BWMthree.printCarInfo = bwmThreePrintCarInfo;
BWMthree.printCarInfo(BWMthree.color,BWMthree.brand,BWMthree.type,BWMthree.year)
;
Car *AodiA6 = new Car();
// AodiA6 = (struct Car*)malloc(sizeof(struct Car));
AodiA6->color = "黑色";
AodiA6->brand = "奥迪";
AodiA6->type = "A6";
AodiA6->year = 2008;
AodiA6->printCarInfo = A6PrintCarInfo;
AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type,AodiA6->year);
return 0;
}

===========================================

成员函数:

像什么结构体实现的 函数指针实际上还是一个成员变量 --> 指针变量 -->不是真正的成员函数

// 成员函数直接使用类内部的成员变量

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
class Car{ //汽车“类”
public:
string color; //颜色
string brand; //品牌
string type; //车型
int year; //年限
// 这些函数指针还是成员变量  --> 指针变量
void (*printCarInfo)(string color,string brand,string type, int year); //函数指针,指向车介绍函数
void (*carRun)(string type); //函数指针,指向车运行的函数
void (*carStop)(string type); //函数指针,执行车停止的函数
// 成员函数
void realPrintCarInfo(){

    string str = "车的品牌是:" + brand
    + ",型号是: " + type
    + ",颜色是:" + color
    + ",上市年限是:" + std::to_string(year);
    cout << str << endl;
}


};
void bwmThreePrintCarInfo(string color,string brand,string type, int year)
{
string str = "车的品牌是:" + brand
+ ",型号是: " + type
+ ",颜色是:" + color
+ ",上市年限是:" + std::to_string(year);
cout << str << endl;
}
void A6PrintCarInfo(string color,string brand,string type, int year)
{
string str = "车的品牌是:" + brand
+ ",型号是: " + type
+ ",颜色是:" + color
+ ",上市年限是:" + std::to_string(year);
cout << str << endl;
}
int main()
{
Car BWMthree;
BWMthree.color = "白色";
BWMthree.brand = "宝马";
BWMthree.type = "3系";
BWMthree.year = 2023;
BWMthree.printCarInfo = bwmThreePrintCarInfo;
BWMthree.printCarInfo(BWMthree.color,BWMthree.brand,BWMthree.type,BWMthree.year);
BWMthree.realPrintCarInfo();

Car *AodiA6 = new Car();
// AodiA6 = (struct Car*)malloc(sizeof(struct Car));
AodiA6->color = "黑色";
AodiA6->brand = "奥迪";
AodiA6->type = "A6";
AodiA6->year = 2008;
AodiA6->printCarInfo = A6PrintCarInfo;
AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type,AodiA6->year);
AodiA6->realPrintCarInfo();
return 0;
}

=====================================================

:: 作用域解析运算符


在 C++ 中,双冒号 :: 称为 "作用域解析运算符"(Scope Resolution Operator)。它用于指定一
个成员(如函数或变量)属于特定的类或命名空间。例如,在类的外部定义成员函数时, :: 用于指明该函数属于哪个类。

=================================


一般用法

 成员函数在类里面定义,在类外实现函数

class Car{ //汽车“类”
public:
string color; //颜色
string brand; //品牌
string type; //车型
int year; //年限
// 这些函数指针还是成员变量  --> 指针变量
void (*printCarInfo)(string color,string brand,string type, int year); //函数指针,指向车介绍函数
void (*carRun)(string type); //函数指针,指向车运行的函数
void (*carStop)(string type); //函数指针,执行车停止的函数
// 成员函数
void realPrintCarInfo();

};


void Car::realPrintCarInfo(){

    string str = "车的品牌是:" + brand
    + ",型号是: " + type
    + ",颜色是:" + color
    + ",上市年限是:" + std::to_string(year);
    cout << str << endl;
}

==========================================

组合:


在 C++中,一个类包含另一个类的对象称为组合(Composition)。这是一种常见的设计模式,用
于表示一个类是由另一个类的对象组成的。这种关系通常表示一种"拥有"("has-a")的关系。


#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;

class Wl{
public:
    string brand;
    int year;

    void printWheelInfo();

};


class Car{ //汽车“类”
public:
string color; //颜色
string brand; //品牌
string type; //车型
Wl wl;
Wl *pwl;

int year; //年限
// 这些函数指针还是成员变量  --> 指针变量
void (*printCarInfo)(string color,string brand,string type, int year); //函数指针,指向车介绍函数
void (*carRun)(string type); //函数指针,指向车运行的函数
void (*carStop)(string type); //函数指针,执行车停止的函数
// 成员函数
void realPrintCarInfo();

};

void Wl::printWheelInfo()
{
  cout<< "轮胎品牌: "<<brand<<"  轮胎年限:  "<<year<<endl;
}


void Car::realPrintCarInfo(){

    string str = "车的品牌是:" + brand
    + ",型号是: " + type
    + ",颜色是:" + color
    + ",上市年限是:" + std::to_string(year);
    cout << str << endl;
}

void bwmThreePrintCarInfo(string color,string brand,string type, int year)
{
string str = "车的品牌是:" + brand
+ ",型号是: " + type
+ ",颜色是:" + color
+ ",上市年限是:" + std::to_string(year);
cout << str << endl;
}
void A6PrintCarInfo(string color,string brand,string type, int year)
{
string str = "车的品牌是:" + brand
+ ",型号是: " + type
+ ",颜色是:" + color
+ ",上市年限是:" + std::to_string(year);
cout << str << endl;
}
int main()
{
Car BWMthree;
BWMthree.pwl= new Wl();

BWMthree.pwl->brand = "米其林";
BWMthree.pwl->year = 1999;


//BWMthree.wl.brand = "米其林";
//BWMthree.wl.year = 1999;

BWMthree.color = "白色";
BWMthree.brand = "宝马";
BWMthree.type = "3系";
BWMthree.year = 2023;
BWMthree.printCarInfo = bwmThreePrintCarInfo;
BWMthree.printCarInfo(BWMthree.color,BWMthree.brand,BWMthree.type,BWMthree.year);
BWMthree.realPrintCarInfo();
//BWMthree.wl.printWheelInfo();

BWMthree.pwl->printWheelInfo();

Car *AodiA6 = new Car();
// AodiA6 = (struct Car*)malloc(sizeof(struct Car));

AodiA6->pwl= new Wl();
AodiA6->pwl->brand = "马牌";
AodiA6->pwl->year = 2003;

//AodiA6->wl.brand = "马牌";
//AodiA6->wl.year = 2003;

AodiA6->color = "黑色";
AodiA6->brand = "奥迪";
AodiA6->type = "A6";
AodiA6->year = 2008;
AodiA6->printCarInfo = A6PrintCarInfo;
AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type,AodiA6->year);
AodiA6->realPrintCarInfo();

//AodiA6->wl.printWheelInfo();
AodiA6->pwl->printWheelInfo();
return 0;
}

new 对象的时候不用 ()  也ok,比如Car *AodiA6 = new Car(); --> Car *AodiA6 = new Car;

====================================================


 

侯捷C++面向对象高级编程之---(9)复习String类的实现过程 头文件 #ifndef __COMPLEX__ // 1.防卫式声明 #define __COMPLEX__ #include <iostream> #include <string.h> using std::strcpy; using std::strlen; using std::ostream; // 2.class head class complex { // class body public: // 3.构造函数 inline complex(double 阅读详情

相关推荐

深入解析SDC时钟约束:从create_clock到set_clock_groups的实战指南

本文深入解析SDC时钟约束,从create_clock到set_clock_groups的实战应用,帮助数字芯片设计工程师掌握时序约束的核心技巧。通过详细讲解时钟三要素(波形定义、不确定性和时钟关系)及实战案例,提升时钟约束的精准性和效率,避免常见设计陷阱。

weixin_29274337的博客 1078

类的实现(c++)

类的完整实现(超级简洁,好上手教程)

qq_61561417的博客 2464

海外最新AI营销AI销售书籍推荐AI营销培训讲师AI销售培训讲师人工智能营销培训师人工智能销售培训师AI赋能营销培训AI赋能销售培训企业AI营销内训企业AI销售内训AI营销课程讲师AI销售课程讲师

专栏|AI营销与AI销售实战 · 第46期在这个人类与算法共舞的时代,销售,已经不是你我熟悉的那个“打电话、见客户、做PPT”的老行当了。你还以为销售是“打感情牌”?错了,情感要继续,但方式已经升级。你还以为AI只是“写写文案”?错了,文案只是皮毛,真正的杀器,是思维方式的重构,是认知地图的重绘。首先,我们需要明确一个事实:AI并不会消灭销售这个职业,但它必然会重新定义销售的本质。在销售领域,真正的制胜之道不是简单地采用AI工具,而是的能力。

Akibfrycc的博客 1101

C++中的类

开发工具与关键技术: Visual Studio / C++ 作者:何文涛 撰写时间:2019-5-15 C++中的类: C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程序设计。类是 C++ 的核心特性,通常被称为用户定义的类型。 类用于指定对象的形式,它包含了数据表示法和用于处理数据的方法。类中的数据和方法称为类的成员。函数在一个类中被称为类的成员 在 C++ 中,类的定...

weixin_44549626的博客 842

别再自己写消息提示了!5分钟用WPF+Material Design打造专业级Snackbar(支持.NET 6)

本文为WPF开发者提供了一份快速集成专业级Snackbar消息提示的实战指南。通过引入MaterialDesignThemes开源库,在.NET 6 WPF项目中,仅需5分钟即可告别传统的MessageBox,实现遵循Material Design规范、动画流畅、支持队列管理的非侵入式Snackbar控件,显著提升应用的用户体验与现代化交互水平。

year5的博客 172

【SABRE 3D, SABRE 3DxT,LAM设备】2025年9月12日-生产期间维护用的双重隔离

生产期间维护用的双重隔离。该程序用于准备将一个或多个双工位设置为维护状态,同时SABRE 3D/3DxT/3D Scimitar保持生产运行。其目的是在不中断生产的情况下将双工位与运行中的设备隔离。

weixin_46735260的博客 574

C++类中类

C++类中类: 类中成员是类 形如下: 第一种方式 class A{ public: class B; }; class A::B { void print() { cout<<“A::B的print()被调用”<<endl; } }; 调用: A::B b; b.print(); 例子: 使用namespace命名空间 定义: class OBJECT::Boy { public: void print() { cout << "Boy" << en

weixin_45312249的博客 2028

升级最新IDEA版本(2022.1.1)

升级最新IDEA版本(2022.1.1)

软件老王 3万+

【复习】C++之实现链表类

#include<iostream> using namespace std; class CList { private: struct Node { int nValue; Node* pNext; }; Node* m_pHead; //链表头尾,长度 Node* m_pEnd; int m_nLength; public: CList() //创...

ddiioopp123的博客 179

ABAP 平台注册 OAuth 2.0 Client 的完整实践指南(覆盖 Authorization Code、SAML Bearer 与 PKCE)

本文详述了在ABAP平台上注册入站OAuth 2.0 Client的关键技术和操作要点。文章面向SAP技术人员,系统梳理了前置条件(TLS/SSL、技术用户、授权对象)、配置路径(SOAUTH2事务)、授权类型差异(授权码、SAML Bearer、PKCE)等重要内容。特别强调入站与出站配置的区别,并提供了保密客户端和公有客户端的配置指引。文中所有事务码和参数均参考SAP官方文档,包含多个实用案例和权限配置建议,为ABAP系统作为OAuth 2.0授权服务器的实施提供全面指导。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 297

CUDA编程00 - 配置CUDA开发环境

第一步:在一台装有Nvidia显卡和驱动的机器上,用nvidia-smi命令查看显卡所支持cuda版本。安装时按提示下一步即可,安装完成用 nvcc -V 命令检查是否成功。第二步: 到Nvidia官网下载CUDA Toolkit并安装,第三步:编写HelloWorld。

GPU全栈博主 1359

【C++复习】实现一个简单的string类【C++面试小题】

本文介绍了如何简单的实现一个string类,即不考虑增删,只考虑资源的申请和释放的string类。

AMor_05的博客 522

基于上下文的 Listwise 重排模型(一):miRNN

2018年阿里团队提出的miRNN(Mutual Influence aware RNN)模型,首次将电商搜索重排建模为全局优化问题,通过RNN捕捉商品序列的相互影响,结合Beam Search生成最优排序,在淘宝搜索中实现GMV(商品交易总额)提升5%的突破。本文将结合论文细节与通俗案例,拆解这一工业级重排框架的核心逻辑。

进一步有进一步的欢喜~ 1918

【C++】string类(学习并实现)(学习复习兼顾)

string类详细讲解,让大家轻松使用STL库,理解STL库,并实现STL库~!

Liyuyue的博客 663

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C++复习——类(class)的实现与定义

一、基本概念 类就是对现实世界中的具有相同属性对象的抽象描述;一般包括属性和方法;分为基类(子类)和派生类。二、类的定义class 类名 { public: //公有的属性和行为 private: //私有的属性和行为 };  需要注意的点:   1) 类名需要遵循一定的命名规则;   2)public和private是属性和行为的关键字;一般类的属性成员应设置为private;p

wangjun_huster的博客 761

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

侯捷C++第六课 complex类实现过程的复习

**1.对于写在头文件中的代码,需要有防卫式定义,**代码如下: #ifndef __你的头文件名__ #define __你的头文件名__ //中间为你要的写入的代码 #endif 2.写出class的body 设计class的数据,并放在private中。 设计class的函数,并放在public中。 #ifndef __你的头文件名__ #define __你的头文件名__ class complex { public: private: double re,im; }; #end

qq_45644529的博客 269

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

侯捷 C++ 课程学习笔记:复习 Complex 类的实现过程

一个复数的形式如下:( a ) 为实部(real part)( b ) 为虚部(imaginary part)( i ) 为虚数单位( ( i^2 = -1 \))我们可以用C++ 类来模拟复数的结构。

努力分享一些人工智能、计算机视觉、影像等相关的知识干货! 468

侯捷C++第九课String类实现的复习

1.类名称选定,数据设计,函数设计 class String { public: String(const char* cstr = 0);//考虑到使用者经常创建不带初值的变量,为其赋予默认初值 String(const String& str); String& operator = (const String& str); ~String(); char* get_c_str() const { return m_data; } private: char* m_d

qq_45644529的博客 188

侯捷 C++ 课程学习笔记:复习 String 类的实现过程

C++ 的类是标准库中的字符串处理类,但在学习 C++ 的过程中,自己实现一个String类是理解等关键知识的重要方式。本笔记详细解析String类的实现过程,包括,并提供详细实例。

努力分享一些人工智能、计算机视觉、影像等相关的知识干货! 459

【C++】string类 _模拟实现 _源码[复习专用]

string类 _ 模拟实现 _源码

qq_63179783的博客 200

【c++笔记(四)侯捷老师课程】复习String类的实现过程——带指针的类

1.防卫式声明 2.类的设计 2.1 需要什么样的数据 字符串需要放很多字符 放数组,数组里面放字符?不可行,数组大小难以确定。 那么就在数据区域放一根指针,将来字符串需要多大,可以用new动态分配一块内存 2.2 需要准备哪些函数开放给外界调用? (1)构造函数(放在public,和class同名,没有返回类型,接收怎样的初值,有无默...

九袋面的博客 383

【c++ 一些比较细节的知识点】函数指针

因为Unity3D项目需要,所以,需要用到DLL来封装一些功能。 常年使用Unity3D,c++基本忘记的差不多了。 现在又要开始第N轮的复习。还有一个原因就是,貌似Unity3D,又走向il2cpp的趋势。。所以,还是赶紧抓回c++吧。 /************************************** 多余的介绍 完成 ****************************

小开KAKA 751

【C++面向对象侯捷】9.复习String类的实现过程

get_c_str()【不改变目的端 的数据,所以函数 + const】(class with pointer 必须考虑的3个函数)拷贝构造【不改变 传入数据的值,所以参数 + const】拷贝赋值【不改变 传入数据的值,所以参数 + const】析构函数【不改变 传入数据的值,所以参数 + const】因为析构函数如此简单,所以要求编译器做成 inline的。【因为比较复杂,所以拉到 class 本体 外 来写】连串赋值,所以需要返回类型!3.构造函数和析构函数。

py今天刷题了吗 116

C++学习/复习14--list的模拟实现(节点类/迭代器封装成类/list类/测试)

生成了一个匿名对象,执行完 `Cat( )` 代码后,此匿名对象就此消失。` 表明 `p` 是一个指向整型的指针常量,这意味着 `p` 始终指向 `a`,但是可以通过 `p` 修改 `a` 的值。- 如果某个指针需要始终指向同一个对象,且该对象的值需要能够被不同部分的代码修改,那么使用指针常量可能更合适。` 表明 `p` 是一个指向整型常量的指针,意味着无法通过指针 `p` 来修改 `a` 的值。,避免在需要长时间存在的对象上使用匿名对象,以免造成不必要的麻烦或错误。` 声明一个指向整型的指针常量。

Hncj2022的博客 686

C++面向对象高级编程(侯捷)—— 9.复习String类的实现过程

class声明 #ifndef __MYSTRING__ // 0.防卫式声明 #define __MYSTRING__ #include <string.h> #include <iostream> using std::ostream; using std::strcpy; using std::strlen; class String // 1.class ...

youshiwukong1524的博客 273
上一篇: C++数组和指针应用实例 -- 实现计算器
下一篇: C++ 复习 -- 重载
mx_jun
博客等级 码龄5年 1605粉丝 148原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值