在c++中使用类
Using class in c++
本文关键字:c++ 更新时间:2023-10-16
我试图使用类,但程序不会运行。怎样才能解决这个问题?请记住,我是c++的初学者。
#include <iostream>
#include <string>
#include <fstream>// enable writing to and reading from files
#include <cstdlib>
#include <time.h>
using namespace std;
//Globals
//----------------------------------------------------------------------------------------------------------------------------
class Person
{
public:
Person() {cout<<"ntBuilding a Person.";}
~Person() {cout<<"ntDestroying a Person.";}
//Accessor Methods
void setPersonName(string X) {PersonName=X; }
void setOccupation(string X) {Occupation=X; }
void setLocation(string X) {Location=X; }
void setReferences(string X) {References=X; }
string GetPersonName() {return PersonName;}
string GetOccupation() {return Occupation;}
string GetLocation() {return Location;}
string GetReferences() {return References;}
private:
string PersonName ;
string Occupation ;
string Location;
string References;
};//----------------------------------------------------------------------------------------------------------------------------
//Function prototypes
void createperson();
void editperson();
void displayperson();
void saveperson();
void loadperson();
Person*CG;
int main (int argc, char *argv[])
{
char choose[10] = "";
CG = new Person ();
cout<<"nt Personnel database 1.0n";
//system ("CLS");
while (choose [0] != 'q')
{
system ("CLS");
cout<<"nt---------------------Main Menu---------------------n";
cout<<"nt| |n";
cout<<"nt| (c) Create person |n";
cout<<"nt| (E) edit information |n";
cout<<"nt| (D) display person |n";
cout<<"nt| (S) Save person |n";
cout<<"nt| (L) Load person |n";
cout<<"nt| (Q) quit |n";
cout<<"nt| |n";
cout<<"nt---------------------------------------------------nnt";
cin>>choose;
//system ("CLS");
switch (choose [0])
{
//system ("CLS");
case 'c': createperson(); break;
case 'e': editperson(); break;
case 'd': displayperson(); system ("pause"); break;
case 's': saveperson(); break;
case 'l': loadperson(); break;
case 'q': cout<<"ntExiting main menu..."; break;
default :"ntInvalid input!";
}
}
system ("pause");
return 0;
}
void createPerson()
{
CG=new Person();
}
//----------------------------------------------------------------------------------------------------------------------------
void editPerson()
{
string TEMP;
system ("CLS");
cout<<"nnt---------------------EDIT person ---------------------------";
cout<<"nt NAME: ";
cin.ignore();
getline (cin,TEMP);
CG->setPersonName(TEMP);
cout<<"nt Occupation: ";
getline (cin,TEMP);
CG->setOccupation(TEMP);
cout<<"nt Location: ";
getline (cin,TEMP);
CG->setLocation(TEMP);
cout<<"nt References: ";
getline (cin,TEMP);
CG->setReferences(TEMP);
}
//----------------------------------------------------------------------------------------------------------------------------
void displayPerson()
{
system("CLS");
cout<<"nnt---------------------person information----------------------";
cout<< "ntName: "<<CG->GetPersonName();
cout<< "ntOccupation: "<<CG->GetOccupation();
cout<< "ntLocation: "<<CG->GetLocation();
cout<< "ntReferences: "<<CG->GetReferences();
cout<<"nnt---------------------------------------------------------------nn";
}
//----------------------------------------------------------------------------------------------------------------------------
void savePerson()
{
try
{
ofstream DATAFILE;
DATAFILE.open("Data1.file",ios::out);
DATAFILE <<CG->GetPersonName ()<<"n";
DATAFILE <<CG->GetOccupation ()<<"n";
DATAFILE <<CG->GetLocation ()<<"n";
DATAFILE <<CG->GetReferences ()<<"n";
DATAFILE.close(); //Prevents data from getting corrupt incase of sudden shutdown
cout<<"nt Success! Data was saved to file.";
}
catch (exception x)
{
cout<<"nt File Error! Could not SAVE PERSON.";
}
}
//----------------------------------------------------------------------------------------------------------------------------
void loadperson()
{
try
{
string TEMP;
ifstream DATAFILE;
DATAFILE.open("Data1.file",ios::in);
getline (DATAFILE,TEMP);
CG->setPersonName ();
getline (DATAFILE,TEMP);
CG->setOccupation ();
getline (DATAFILE,TEMP);
CG->setLocation ();
getline (DATAFILE,TEMP);
CG->setReferences ();
DATAFILE.close();
}
catch (exception x)
{
cout<<"nt File Error! Unable to load data.";
}
}
//----------------------------------------------------------------------------------------------------------------------------
5 IntelliSense: too few arguments in function call c:userstoshibadocumentsvisual studio 2010projectstrial and errortrial and errorbel class.cpp 136
6 IntelliSense: too few arguments in function call c:userstoshibadocumentsvisual studio 2010projectstrial and errortrial and errorbel class.cpp 138
7 IntelliSense: too few arguments in function call c:userstoshibadocumentsvisual studio 2010projectstrial and errortrial and errorbel class.cpp 140
8 IntelliSense: too few arguments in function call c:userstoshibadocumentsvisual studio 2010projectstrial and errortrial and errorbel class.cpp 142
错误1:'Person::setPersonName':函数不接受0个参数c:userstoshibadocumentsvisual studio 2010projects试错试错bel class.cpp 136c:userstoshibadocumentsvisual studio 2010projectstrial and Error trial and Error bel class.cpp 138 .c: 'Person:: setooccupation ':函数不接受0个参数c:userstoshibadocumentsvisual studio 2010projectstrial and Error trial and Error bel class.cpp 140 .c: 'Person::setLocation':函数不接受0个参数c:userstoshibadocumentsvisual studio 2010projects试错试错bel class.cpp 142
关键字private
和public
的拼写是这样的,而不是大写的首字母
此外,拥有一个成员int Person;
的类Person似乎是一个很好的方法来获得一些混乱。
相关文章:
- 没有找到相关文章