⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 练习.txt

📁 钱能主编 C++程序设计教程(第一版) 该书习题的答案代码
💻 TXT
字号:
11.2
//*********************
//**      Cat.h      **
//*********************
class Cat
{
public:
	int GetAge();
	void SetAge(int age);
	void Meow();
protected:
	int itsAge;
};

//***********************
//**      Cat.cpp      **
//***********************
#include <iostream.h>
#include "Cat.h"

int Cat::GetAge()
{
	return itsAge;
}

void Cat::SetAge(int age)
{
	itsAge=age;
}

void Cat::Meow()
{
	cout<<"Meow."<<endl;
}

//************************
//**      main.cpp      **
//************************
#include <iostream.h>
#include "Cat.h"

void main()
{
	Cat frisky;
	frisky.SetAge(5);
	frisky.Meow();
	cout<<"frisky is a cat who is "
		<<frisky.GetAge()
		<<" years old."<<endl;
	frisky.Meow();
}

11.3
//**********************
//**      Date.h      **
//**********************
//class declaration
class Date
{
public:
	void Set(int d,int m,int y);
	void Disp();
	void IncOneDay();

protected:
	int day;	
	int month;
	int year;
};

//************************
//**      Date.cpp      **
//************************
#include <iostream.h>
#include "Date.h"

void Date::Set(int d,int m,int y)
{
	day=d;
	month=m;
	year=y;
}

void Date::Disp()
{
	cout<<day<<"/"<<month<<"/"<<year<<endl;
}

void Date::IncOneDay()
{
	day+=1;
}

//************************
//**      main.cpp      **
//************************
#include <iostream.h>
#include "Date.h"

void main()
{
	Date current;
	current.Set(15,3,1998);
	current.Disp();
	current.IncOneDay();
	current.Disp();
}

11.4
//**********************
//**      Time.h      **
//**********************
//class time.h declaration
#include <iostream.h>

class Time
{
public:
	void Set(int h,int m,int s)
	{
		hour=h;
		minute=m;
		second=s;
	}

	void Disp()
	{
		cout<<"time	"<<hour<<":"<<minute<<":"<<second<<endl;
	}

protected:
	int hour;
	int minute;
	int second;
};

//************************
//**      main.cpp      **
//************************
//#include <iostream.h>
#include "Time.h"

void main()
{
	Time cur;
	cur.Set(20,22,22);
	cur.Disp();
}

11.5
//***********************
//**      Stack.h      **
//***********************
//class Stack.h declaration
struct Node
{
	int a;
	Node* next;
};

class Stack
{
public:
	void put(int item);
	int get();

protected:
	Node* tmp;
};    //the symble ";" here is very important!!!

//*************************
//**      Stack.cpp      **
//*************************
#include "Stack.h"

void Stack::put(int item)
{
	Node* n=new Node;
	n->a=item;
	n->next=tmp;
	tmp=n;
}

int Stack::get()
{	
	int tmp_val;
	tmp_val=tmp->a;
	tmp=tmp->next;
	return tmp_val;
}

//************************
//**      main.cpp      **
//************************
#include <iostream.h>
#include "Stack.h"

void main()
{
	Stack st;

	st.put(10);
	st.put(12);
	st.put(14);

	cout<<st.get()<<endl;
	cout<<st.get()<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -