类与对象使用

#include<iostream>
using namespace std;

class Date
{
public:
	void Init(int year, int month, int day);
	void Init0(int _year, int _month, int _day);
	void Show();

	//构造函数 ?创建时自动调用
	//1.无参构造函数
	Date()//:_year(2003),_month(3),_day(8) ?初始化列表
	{
		_year = 2024;
		_month = 11;
		_day = 4;
	}
	//2.带参构造函数
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	//析构函数 ?当对象超出其作用域或被显性删除后自动调用 ? 析构顺序 ?在局部作用域中,多个对象按定义的逆序进行析构
	~Date()
	{
		cout << "这是析构函数" << endl;
	}

	//拷贝构造函数
	Date(const Date& d) ?//const修饰的函数参数是指针时,代表 在函数体内不能修改该指针所指的内容,起到保护作用
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	//this在链式语句中,返回this可以实现对同一对象的连续操作
	Date& set_year(int year)
	{
		_year = year;
		return *this;
	}

	Date& set_month(int month)
	{
		_month = month;
		return *this;
	}

	Date& set_day(int day)
	{
		_day = day;
		return *this;
	}
private:
	int _year;
	int _month;
	int _day;

};
// 类的作用域
void Date::Init(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}

void Date::Init0(int _year, int _month, int _day) ? //this避免命名冲突
{
	this->_year = _year;
	this->_month = _month;
	this->_day = _day;
}

void Date::Show()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}

void main()
{
	//Date date; ? ? //无参构造函数
	//Date date1(2025,11,4); ? //有参构造函数
	//date.Init(2024, 11, 4);
	//date.Show();
	//date.Init0(2024, 11, 4);
	//date.Show();
	//date.set_year(2025).set_month(1).set_day(1).Show();
//?? ?delete &date;

	Date date1(2025, 11, 4);
	Date date2 = date1;

	date1.Show();
	date2.Show();
}



//-------------------------------------------------------------------------------------------------------------------------------- -
//
//计算器类

#include<iostream>
using namespace std;

class Counter
{
public:
	Counter(int num);
	void increament();
	void decrement();
	int getValue();
	void print();
private:
	int value;
};

Counter::Counter(int num) :value(num)
{

}
void Counter::increament()
{
	value++;
}

void Counter::decrement()
{
	value--;
}

int Counter::getValue()
{
	return value;
}

void Counter::print()
{
	cout << "value=" << value << endl;
}

void main()
{
	int n1, n2;
	cin >> n1;
	Counter counter(n1);
	counter.print();
	counter.decrement();
	counter.print();
	n2 = counter.getValue();
	cout << "value=" << n2 << endl;
	counter.increament();
	counter.print();

}



//Date类

#include<iostream>
using namespace std;

class Date
{
public:
	void setYear(int y);
	void setMonth(int m);
	void setDay(int d);
	bool isLeapYear();
	void printDate();
private:
	int year, month, day;
};

void Date::setYear(int y)
{
	year = y;
}

void Date::setMonth(int m)
{
	month = m;
}

void Date::setDay(int d)
{
	day = d;
}

bool Date::isLeapYear()
{
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		return 1;
	else
		return 0;
}

void Date::printDate()
{
	cout << "日期为:" << year << "-" << month << "-" << day << endl;
}

void main()
{
	Date date;
	int y, m, d;
	cin >> y >> m >> d;
	date.setYear(y);
	date.setMonth(m);
	date.setDay(d);
	date.printDate();
	if (date.isLeapYear() == 1)
		cout << "该年为闰年!" << endl;
	else
		cout << "该年不为闰年!" << endl;
}



//Cylinder类

#include<iostream>
using namespace std;
#define pi 3.1415926

class Cylinder
{
private:
	double r, h, v;
public:
	Cylinder(double r, double h);
	void showVolume();
};

Cylinder::Cylinder(double r, double h)
{
	this->r = r;
	this->h = h;
	v = h * pi * this->r * this->r;
}

void Cylinder::showVolume()
{
	cout << "体积为:" << v << endl;
}

void main()
{
	double a, b;
	cout << "请输入半径和高:" << endl;
	cin >> a >> b;
	Cylinder volume(a, b);
	volume.showVolume();
}



//Book类

#include<iostream>
#include<string>
using namespace std;


class Book
{
private:
	string name;
	string writer;
	int qu;
	int price;
public:
	Book(string n = "", string w = "", int q = 0);
	void Book_set(string n, string w, int q);
	void print_book();
};

Book::Book(string n, string w, int q)
{
	name = n;
	writer = w;
	qu = q;
	price = qu * 10;
}
void Book::Book_set(string n, string w, int q)
{
	name = n;
	writer = w;
	qu = q;
	price = qu * 10;
}
void Book::print_book()
{
	cout << "书名:" << name << "\t" << "作者:" << writer << "\t" << "qu*price:" << price << "\t" << endl;
}


void main()
{
	string name, writer;
	int qu, i;
	Book a[5];
	Book* p;
	p = a;
	p = p + 4;
	cout << "请输入五个数据:" << endl;
	for (i = 0; i < 5; i++)
	{
		cin >> name >> writer >> qu;
		a[i].Book_set(name, writer, qu);
	}
	cout << "顺序显示的结果为:" << endl;
	for (i = 0; i < 5; i++)
	{
		a[i].print_book();
	}
	cout << "使用指针,逆序显示的结果为:" << endl;
	for (i = 0; i < 5; i++)
	{
		(*p).print_book();
		p--;
	}


}



//Stock类:

#include<iostream>
#include<string>
using namespace std;


class Stock
{
private:
	string stockcode;
	int quan;
	double price;
public:
	Stock(string a, int q = 1000, double p = 9.98);
	void print();
};

Stock::Stock(string a, int q, double p)
{
	stockcode = a;
	quan = q;
	price = p;
}

void Stock::print()
{
	cout << "stockcode:" << stockcode << "\t"
		<< "quan:" << quan << "\t"
		<< "price:" << price << "\t" << endl;
}


void main()
{
	string s1, s2; int q1; double p1;
	cin >> s1 >> q1 >> p1;
	cin >> s2;
	Stock S1(s1, q1, p1);
	Stock S2(s2);
	S1.print();
	S2.print();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值