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

📄 list0605.cpp

📁 《21天学通C++》Teach Yourself C++ in 21 Days (Fourth Edition) 源代码
💻 CPP
字号:
// Demonstrates compiler errors
// This program does not compile!
#include <iostream>          // 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;
   std::cout << "Cat Constructor\n";
}
  
Cat::~Cat()                   // destructor, takes no action
{
   std::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()
{
   std::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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -