实例001——实现C++类的多重继承

程序目录:

程序源码: 

#include "pch.h"
#include <iostream>

using namespace std;
class CTimeType//定义时间类
{
	int hour, minute, second;              							//成员变量
public:
	CTimeType(int h = 12, int m = 0, int s = 0)								//构造函数
	{
		hour = h;
		minute = m;
		second = s;
	}
	void display()												//成员函数,输出时间
	{
		cout << hour << ":" << minute << ":" << second << endl;
	}
	void SetTime(int h, int m, int s)								//成员函数,设置时间
	{
		hour = h;
		minute = m;
		second = s;
	}
};
class CDateType//日期类
{
	int month, day, year;                							//成员变量
public:
	CDateType(int mon = 1, int d = 1, int y = 2008)							//构造函数
	{
		month = mon;
		day = d;
		year = y;
	}
	void display()											//成员函数,输出日期
	{
		cout << month << "/" << day << "/" << year << endl;
	}
	void SetDate(int mon, int d, int y)								//成员函数,设置日期
	{
		month = mon;
		day = d;
		year = y;
	}
};
class CDateTimeType :public CDateType, public CTimeType//时间日期类
{
public:
	CDateTimeType(int mon = 1, int d = 1, int y = 2000, int h = 0, int m = 0, int s = 0) :CDateType(mon, d, y), CTimeType(h, m, s) {}								//构造函数
	void display()//成员函数,显示时间、日期
	{
		CDateType::display();						//调用CDateType类的display函数
		CTimeType::display();						//调用CTimeType类的display函数
	}
};

int main()
{
	cout << "类的多重继承演示" << endl;
	CDateTimeType dt(1, 1, 2008, 11, 12, 12);		//直接使用DateTimeType构造函数设置日期时间
	cout << "调用CDateTimeType类构造函数设定的初始日期、时间为:" << endl;
	dt.display();//显示时间日期
	dt.SetDate(8, 8, 2008);						//调用基类的成员函数修改日期
	dt.SetTime(20, 8, 8);						//调用基类的成员函数修改时间
	cout << "调用基类成员函数修改后的日期、时间为:" << endl;
	dt.display();
	return 0;
}

运行结果:

资源:

          https://download.csdn.net/download/sunjikui1255326447/12054849 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
感知机是二分的线性分模型,其输入为实例的特征向量,输出为实例别,取 +1 和 -1 二值。感知机对应于输入空间(特征空间)中将实例划分为正负两的分离超平面,属于判别模型。 感知机学习算法是基于随机梯度下降法的。具体地,首先任意选取一个超平面,然后用梯度下降法不断地极小化目标函数,找出最优超平面。 以下是感知机算法的C++实现案例代码: ```c++ #include <iostream> #include <vector> #include <random> using namespace std; class Perceptron { public: Perceptron(int feature_num) : w(feature_num), b(0) {} void train(const vector<vector<double>>& X, const vector<double>& y, int max_iter = 100) { int n_samples = X.size(); int n_features = X[0].size(); mt19937 rng(0); uniform_int_distribution<int> dist(0, n_samples - 1); for (int iter = 0; iter < max_iter; iter++) { int i = dist(rng); double wx = 0; for (int j = 0; j < n_features; j++) { wx += X[i][j] * w[j]; } double yi = y[i]; if (yi * (wx + b) <= 0) { for (int j = 0; j < n_features; j++) { w[j] += yi * X[i][j]; } b += yi; } } } double predict(const vector<double>& x) { double wx = 0; int n_features = x.size(); for (int i = 0; i < n_features; i++) { wx += x[i] * w[i]; } return wx + b > 0 ? 1 : -1; } void print_weights() const { cout << "w = ["; for (double wi : w) { cout << wi << ", "; } cout << "], b = " << b << endl; } private: vector<double> w; double b; }; int main() { vector<vector<double>> X{ {3, 3}, {4, 3}, {1, 1} }; vector<double> y{1, 1, -1}; Perceptron model(X[0].size()); model.train(X, y); model.print_weights(); cout << "predict([3, 4]) = " << model.predict({3, 4}) << endl; return 0; } ``` 在上述代码中,Perceptron代表感知机模型。train函数接受训练数据X和y,以及最大迭代次数max_iter,默认为100。predict函数接受一个样本特征向量x,返回其预测的别标签。print_weights函数打印训练后得到的权重和偏置项。 本例中,使用学习率为1的随机梯度下降法进行模型训练。训练数据X是一个3x2的矩阵,y是一个包含3个元素的向量,表示3个样本的别标签。模型训练完毕后,使用predict函数对特定样本进行预测。 以上是感知机算法的C++实现案例代码,希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值