我已经阅读了很多有关C++类的教程,但它们漏掉了其他教程包括的一些内容。
请问有人可以展示一下如何编写和使用一个非常简单的C++类,其中包含可见性、方法以及一个简单的构造函数和析构函数吗?
我已经阅读了很多有关C++类的教程,但它们漏掉了其他教程包括的一些内容。
请问有人可以展示一下如何编写和使用一个非常简单的C++类,其中包含可见性、方法以及一个简单的构造函数和析构函数吗?
以下示例来自于C++中的构造函数和析构函数,并对其进行了更好的解释:
#include <iostream> // for cout and cin
class Cat // begin declaration of the class
{
public: // begin public section
Cat(int initialAge); // constructor
Cat(const Cat& copy_from); //copy constructor
Cat& operator=(const Cat& copy_from); //copy assignment
~Cat(); // destructor
int GetAge() const; // accessor function
void SetAge(int age); // accessor function
void Meow();
private: // begin private section
int itsAge; // member variable
char * string;
};
// constructor of Cat,
Cat::Cat(int initialAge)
{
itsAge = initialAge;
string = new char[10]();
}
//copy constructor for making a new copy of a Cat
Cat::Cat(const Cat& copy_from) {
itsAge = copy_from.itsAge;
string = new char[10]();
std::copy(copy_from.string+0, copy_from.string+10, string);
}
//copy assignment for assigning a value from one Cat to another
Cat& Cat::operator=(const Cat& copy_from) {
itsAge = copy_from.itsAge;
std::copy(copy_from.string+0, copy_from.string+10, string);
}
// destructor, just an example
Cat::~Cat()
{
delete[] string;
}
// GetAge, Public accessor function
// returns value of itsAge member
int Cat::GetAge() const
{
return itsAge;
}
// 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";
}
// create a cat, set its age, have it
// meow, tell us its age, then meow again.
int main()
{
int Age;
cout<<"How old is Frisky? ";
cin>>Age;
Cat Frisky(Age);
Frisky.Meow();
cout << "Frisky is a cat who is " ;
cout << Frisky.GetAge() << " years old.\n";
Frisky.Meow();
Age++;
Frisky.SetAge(Age);
cout << "Now Frisky is " ;
cout << Frisky.GetAge() << " years old.\n";
return 0;
}
void SetAge(int age);
不是一个访问器而是一个修改器,对吗? - Yeo即使他是一个学生,也值得尝试回答,因为这是一个复杂的问题,对于C++新访问者来说并不容易 :)
C++中的类服务于两种设计范式的交集,
1)ADT :: 基本上意味着一个新类型,例如整数'int'或实数'double',甚至是像“日期”这样的新概念。在这种情况下,简单的类应该如下所示,
class NewDataType
{
public:
// public area. visible to the 'user' of the new data type.
.
.
.
private:
// no one can see anything in this area except you.
.
.
.
};
这是一个ADT最基本的骨架...当然,你可以忽略公共区域,或者删除访问修饰符(public、private),使整个类型变为私有的。但那只是无意义的,因为NewDataType就变得没用了!可以想象一下一个“int”,你可以声明但不能对它进行任何操作。
因此,你需要一些有用的工具,这些工具基本上并不需要NewDataType存在,但你会使用它们来让你的类型看起来像语言中的任何“原始”类型。
第一个工具是构造函数。在语言的许多地方都需要构造函数。看看int,让我们试着模仿它的行为。
int x; // default constructor.
int y = 5; // copy constructor from a 'literal' or a 'constant value' in simple wrods.
int z = y; // copy constructor. from anther variable, with or without the sametype.
int n(z); // ALMOST EXACTLY THE SAME AS THE ABOVE ONE, it isredundant for 'primitive' types, but really needed for the NewDataType.
以上每一行都是一个声明,变量就在那里被构造。
最后想象一下上面的int变量在一个名为“fun”的函数中,该函数被调用。
int fun()
{
int y = 5;
int z = y;
int m(z);
return (m + z + y)
// the magical line.
}
你可以看到这条神奇的语句,在这里你可以告诉编译器任何你想要的! 当你完成每件事情并且你的NewDataType在本地范围内不再有用,比如在函数中,你就需要杀死它。 一个经典的例子就是释放通过 'new' 保留的内存!
所以我们非常简单的 NewDataType 变成了:
class NewDataType
{
public:
// public area. visible to the 'user' of the new data type.
NewDataType()
{
myValue = new int;
*myValue = 0;
}
NewDataType(int newValue)
{
myValue = new int;
*myValue = newValue;
}
NewDataType(const NewDataType& newValue){
myValue = new int;
*myValue = newValue.(*myValue);
}
private:
// no one can see anything in this area except you.
int* myValue;
};
现在这是非常基本的骨架,要开始构建有用的类,您必须提供公共函数。
在C++中构建类时需要考虑许多微小的工具,
... ... ...
2)Object :: 基本上意味着一个新类型,但不同的是它属于兄弟姐妹、祖先和后代。看看C++中的“double”和“int”,“int”是“double”的子集,因为每个“int”至少在概念上都是一个“double” :)
class A
{
public:
// a simple constructor, anyone can see this
A() {}
protected:
// a simple destructor. This class can only be deleted by objects that are derived from this class
// probably also you will be unable to allocate an instance of this on the stack
// the destructor is virtual, so this class is OK to be used as a base class
virtual ~A() {}
private:
// a function that cannot be seen by anything outside this class
void foo() {}
};
#include <iostream>
#include <string>
class Simple {
public:
Simple(const std::string& name);
void greet();
~Simple();
private:
std::string name;
};
Simple::Simple(const std::string& name): name(name) {
std::cout << "hello " << name << "!" << std::endl;
}
void Simple::greet() {
std::cout << "hi there " << name << "!" << std::endl;
}
Simple::~Simple() {
std::cout << "goodbye " << name << "!" << std::endl;
}
int main()
{
Simple ton("Joe");
ton.greet();
return 0;
}
有些傻,但是就是这样。请注意,“visibility”是一个误称:公共和私有控制可访问性,但即使“private”也可以从外部“visible”,只是不可访问(尝试访问它是错误的)。