5.5-set函数界面(错误).cpp

来自「Visual C++的课件」· C++ 代码 · 共 65 行

CPP
65
字号
// Demonstrates compiler errors


#include <iostream.h>          // for cout

class Cat
{
public:
	Cat(int initialAge);
	~Cat();
	int GetAge() const;          // const accessor function
	void SetAge (int age);
	void Meow();
private:
	int itsAge;
};

// constructor of Cat,
Cat::Cat(int initialAge)
{
	itsAge = initialAge;
	cout << "Cat Constructor\n";
}

Cat::~Cat()                   // destructor, takes no action
{
	cout << "Cat Destructor\n";
}
// GetAge, const function
// but we violate const!
int Cat::GetAge() const
{
	return (itsAge++);         // violates const!
}

// definition of SetAge, public
// accessor function

void Cat::SetAge(int age)
{
	// set member variable its age to
	// value passed in by parameter age
	itsAge = age;
}

// definition of Meow method
// returns: void
// parameters: None
// action: Prints "meow" to screen
void Cat::Meow()
{
	cout << "Meow.\n";
}

// demonstrate various violations of the
// interface, and resulting compiler errors
int main()
{
	Cat Frisky;                 // doesn't match declaration
	Frisky.Meow();
	Frisky.Bark();              // No, silly, cat's can't bark.
	Frisky.itsAge = 7;          // itsAge is private
	return 0;
}

⌨️ 快捷键说明

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