c++学习-基础-类的构造函数

/*
	Description: 类的构造函数
	构造函数是特殊的成员函数
	构造函数的定义
	构造函数的初始化列表
	合成的默认构造函数 
*/
#include<iostream>

using namespace std;

class Person
{
public:
	//默认的构造函数 
	Person():money(0) 
	{
		//money = 0;
		//可以空着	
	}
	
public:
	int money;
};

int main()
{
	//a ,b 都是对象 
	Person a;		//创建对象a时,通过调用Person的构造函数创建对象 
	Person b;
	
	cout<<a.money<<endl;
	
	cout<<b.money<<endl;
	
	return 0;
}
#include<iostream>

using namespace std;

class Dog
{
public:
	Dog():count(0),name("bill"){
	}
public:
	int count;
	string name;
};

int main()
{
	cout<<"hello,构造函数"<<endl; 
	
	Dog a;//创建对象a 
	
	cout<<a.count<<a.name<<endl;

	return 0;
}
#ifndef _SALES_ITEM_H
#define _SALES_ITEM_H
#include<string>
//销售 项目 
class Sales_item		//class要变成对象才可以使用! 
{
public:
	double avg_price() const;	//函数原型声明 
	bool same_isbn(const Sales_item &rhs) const;		 //这样的形参是是什么意思?const  Sales_item是变量的类型  引用 
//private:

public:
	Sales_item():units_sold(0),revenue(0){} 
	
public:
	std::string isbn;//书号
	unsigned units_sold;//数量
	double revenue; 
};

#endif
#include<iostream>

#include "20210302003.h"
using namespace std;

int sum(int x,int y)
{
	return x+y;	
} 

double Sales_item::avg_price() const
{
	if(this->units_sold != 0)
		return (this->revenue/this->units_sold);
	else
		return 0;
}

bool Sales_item::same_isbn(const Sales_item &rhs) const	
{
	//      当前对象 	
	return this->isbn == rhs.isbn;
}

int main()
{
	//类class   对象 
	Sales_item item1,item2;
	
	item1.isbn = "0-201-78345-x";
	item1.units_sold = 10;
	item1.revenue = 300.00;
	
	cout<<item1.avg_price()<<endl;
	
	item2.isbn = "0-201-78345-x";
	item2.units_sold = 2;
	item2.revenue = 70.00;
	
	cout<<item2.avg_price()<<endl;
	
	if(item1.same_isbn(item2))
	{
		cout<<"这两个sales item是同一种书!"<<endl;
	}else{
		cout<<"这两个sales item不是同一种书!"<<endl;
	}

	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值