C++学习笔记——类与对象

关键词:类、对象指针、函数指针

#include<iostream>
using namespace std;
class Date{
	public:
		int year,month,day;
		void init(int y,int m,int d);
		void print();
}; 
void Date::init(int yy,int mm,int dd){
	year=yy;
	month=mm;
	day=dd;
}
void Date::print(){
	cout<<year<<'-'<<month<<'-'<<day<<endl;
}
int main(){
	Date date1;
	Date *p1=&date1; //对象指针 
	p1->init(2006,5,13);
	p1->print();
	int *p2=&date1.year; 
	cout<<*p2<<endl;
	void(Date::*p3)(int,int,int); //有参函数指针 
	void(Date::*p4)(); //无参函数指针 
	p3=Date::init;
	p4=Date::print;
	(date1.*p3)(2006,4,8);
	(date1.*p4)();
	return 0;
}

运行结果:
2006-5-13
2006
2006-4-8

上一篇:WPF教程一:基础


下一篇:HDUOJ 2006求奇数的乘积