クラスの操作を開始したばかりで、このコードでは、ユーザーから文字列を取得し、その長さをカウントしてから出力するクラスを作成しようとしています。しかし、何のためにあるのか理解できないエラーがいくつか発生しています。助けていただければ幸いです。
#include<iostream>
using namespace std;
class String
{
public:
String(); // constructor
void Print(); // printing the string
void Get(); // getting the string from the user.
int CountLng( char *); // counting length
~String(); // destructor
private:
char *str;
int lng;
} str1;
String::String()
{
lng=0;
str=NULL;
}
int CountLng( char *str)
{
char * temp;
temp=str;
int size=0;
while( temp !=NULL)
{
size++;
temp++;
}
return size;
};
void String::Get()
{
str= new char [50];
cout<<"Enter a string: "<<endl;
cin>>str;
};
void String::Print()
{
cout<<"Your string is : "<<str<<endl<<endl;
lng= CountLng( str);
cout<<"The length of your string is : "<<lng<<endl<<endl;
};
int main()
{
str1.Get();
str1.Print();
system("pause");
return 0;
}