newstr.cpp
来自「本课程主要介绍面向对象程序设计的方法和c++语言的基本概念。以c++语言中的面向」· C++ 代码 · 共 37 行
CPP
37 行
// newstr.cpp
// using new to get memory for strings
#include <iostream>
#include <cstring> //for strcpy(), etc
using namespace std;
////////////////////////////////////////////////////////////////
class String //user-defined string type
{
private:
char* str; //pointer to string
public:
String(char* s) //constructor, one arg
{
int length = strlen(s); //length of string argument
str = new char[length+1]; //get memory
strcpy(str, s); //copy argument to it
}
~String() //destructor
{
cout << "Deleting str\n";
delete[] str; //release memory
}
void display() //display the String
{
cout << str << endl;
}
};
////////////////////////////////////////////////////////////////
int main()
{ //uses 1-arg constructor
String s1 = "Who knows nothing doubts nothing.";
cout << "s1="; //display string
s1.display();
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?