1348 [BA1000] The Student Class with Public Data Fields
2017-05-28 12:23
465 查看
Description
With a class "Student" declared as below: class Student { public: int id; char name[50]; // Data field int age; // Data field Student(); Student(int, char*, int); }; You are required to implement the class functions and also two other functions used to process Student objects, which can get output as specified later. void set(Student &, int, char*, int); void print(Student); 1) You don't need to submit the main function. 2) You don't need to include the header file for class declaration by yourself.
Output
Steven Gates (100) is 61 years old. Larry Jordan (123) is 18 years old. No Name (124) is 0 years old.
提示,头文件请包涵如下代码:
#include<iostream> #include<cstring> using namespace std; class Student { public: int id; char name[50]; // Data field int age; // Data field Student(); Student(int, char*, int); //void set(int, char*, int); //void print(); }; void set(Student &, int, char*, int); void print(Student);
Provided Codes
framework.cpp
#include <iostream> #include <cstring> #include "source.h" using namespace std; int main() { Student std1, std2(123, "Larry Jordan", 18), std3(124); set(std1, 100, "Steven Gates", 61); print(std1); print(std2); print(std3); return 0; }
Submission
source.h
#include <iostream> #include <cstring> using namespace std; class Student { public: int id; char name[50]; // Data field int age; // Data field Student(); Student(int, char*, int); }; Student::Student(){ id=0; strcpy(name,"No Name"); age=0; } Student::Student(int Id,char* Name="No Name",int Age=0){ id=Id; strcpy(name,Name); age=Age; } void set(Student &stu, int Id, char* Name, int Age){ stu.id=Id; strcpy(stu.name,Name); stu.age=Age; } void print(Student stu){ cout<<stu.name<<" ("<<stu.id<<") is "<<stu.age<<" years old."<<endl; }
Standard Answer
source.h
#include <iostream> #include <cstring> using namespace std; class Student { public: int id; char name[50]; // Data field int age; // Data field Student(); Student(int, char*, int); //void set(int, char*, int); //void print(); }; void set(Student &, int, char*, int); void print(Student); Student::Student() { id=0; strcpy(name,"No Name"); age=0; } Student::Student(int pid,char* pname="No Name",int page=0) { id=pid; strcpy(name,pname); age=page; } void set(Student&stu,int id,char*name,int age) { stu.id=id; stu.age=age; strcpy(stu.name,name); } void print(Student stu) { cout<< stu.name <<" ("<<stu.id<<") "<<"is "<<stu.age<<" years old."<<endl; }
相关文章推荐
- The Student Class with Private Data fields (for lab)
- Subscriber class xxx.xxx.xxx and its super classes have no public methods with the@Subscribe annotat
- EventBusException: Subscriber class *** and its super classes have no public methods with the @Subsc
- EventBusException: Subscriber class *** and its super classes have no public methods with the @Subsc
- StructLayoutAttribute- control the physical layout of the data fields of a class or structure.
- Subscriber class xxx.xxx.xxx and its super classes have no public methods with the@Subscribe annotat
- EventBusException: Subscriber class *** and its super classes have no public methods with the @Subsc
- Working with Data in ASP.NET 2.0 :: Using TemplateFields in the GridView Control
- [React] Public Class Fields with React Components
- the differences of DataRelation class between 1.1 and 2.0
- Lesson 05: Working with Disconnected Data - The DataSet and SqlDataAdapter(使用断开数据--DataSet和SqlDataAdapter)
- Implementing Virtual Mode with Just-In-Time Data Loading in the Windows Forms DataGridView Control
- How to: Send Data Using the WebRequest Class
- Using ASP.NET 3.5's ListView and DataPager Controls: Grouping Data with the ListView Control (翻译)
- Lesson 04: Reading Data with the SqlDataReader(使用SqlDataReader读取数据)
- Complexities with NHibernate, DataTransferObjects and the Assembler Pattern(Ben Scott's Blog)
- CTreeCtrl/CListCtrl/CListBox With ToolTip Based On the Item Data
- ASP.NET操作EXCEL时出现的错误 Retrieving the COM class factory for component with CLSID(转)
- How to populate the datagrid on background thread with data binding by using Visual C#
- AS.NET2.0 用户控件错误!!The base class includes the field 'MyControl_1', but its type (MyControl) is not compatible with the type of control (ASP.MyControl_ascx).