在C++里,想用自己定义的类创建一个数组,每一个数组元素都是这个类,,就是这个程序..出错.
#include<iostream>#include<string>usingnamespacestd;classSTUDENT{stringSName;doubleGr...
#include<iostream>
#include<string>
using namespace std;
class STUDENT
{
string SName;
double Grade;
public:
STUDENT(string n,double g)
{
SName=n;
Grade=g;
}
void printS()
{
cout<<SName<<" "<<Grade<<endl;
}
};
class TEACHER
{
string TName;
string Title;
public:
TEACHER(string tn,string t)
{
TName=tn;
Title=t;
}
void printT()
{
cout<<TName<<" "<<Title<<endl;
}
};
void main()
{
STUDENT A[2]={{"Liyc",100},{"Zengyk",60}};
TEACHER B[2]={{"BroL","man"},{"BroZ","man"}};
A[0].printS();
A[1].printS();
B[0].printT();
B[1].printT();
system("pause");
} 展开
#include<string>
using namespace std;
class STUDENT
{
string SName;
double Grade;
public:
STUDENT(string n,double g)
{
SName=n;
Grade=g;
}
void printS()
{
cout<<SName<<" "<<Grade<<endl;
}
};
class TEACHER
{
string TName;
string Title;
public:
TEACHER(string tn,string t)
{
TName=tn;
Title=t;
}
void printT()
{
cout<<TName<<" "<<Title<<endl;
}
};
void main()
{
STUDENT A[2]={{"Liyc",100},{"Zengyk",60}};
TEACHER B[2]={{"BroL","man"},{"BroZ","man"}};
A[0].printS();
A[1].printS();
B[0].printT();
B[1].printT();
system("pause");
} 展开
2个回答
展开全部
数组初始化错误,改完代码如下:
#include<iostream>
#include<string>
using namespace std;
class STUDENT
{
string SName;
double Grade;
public:
STUDENT(string n,double g)
{
SName=n;
Grade=g;
}
void printS()
{
cout<<SName<<" "<<Grade<<endl;
}
};
class TEACHER
{
string TName;
string Title;
public:
TEACHER(string tn,string t)
{
TName=tn;
Title=t;
}
void printT()
{
cout<<TName<<" "<<Title<<endl;
}
};
void main()
{
STUDENT A[2]={STUDENT("Liyc",100),STUDENT("Zengyk",60)}; //应该这样初始化
TEACHER B[2]={TEACHER("BroL","man"),TEACHER("BroZ","man")};
A[0].printS();
A[1].printS();
B[0].printT();
B[1].printT();
system("pause");
}
#include<iostream>
#include<string>
using namespace std;
class STUDENT
{
string SName;
double Grade;
public:
STUDENT(string n,double g)
{
SName=n;
Grade=g;
}
void printS()
{
cout<<SName<<" "<<Grade<<endl;
}
};
class TEACHER
{
string TName;
string Title;
public:
TEACHER(string tn,string t)
{
TName=tn;
Title=t;
}
void printT()
{
cout<<TName<<" "<<Title<<endl;
}
};
void main()
{
STUDENT A[2]={STUDENT("Liyc",100),STUDENT("Zengyk",60)}; //应该这样初始化
TEACHER B[2]={TEACHER("BroL","man"),TEACHER("BroZ","man")};
A[0].printS();
A[1].printS();
B[0].printT();
B[1].printT();
system("pause");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询