The Student Class with Private Data fields (for lab)
2016-09-05 10:58
507 查看
The Student Class with Private Data fields (for lab)
标签(空格分隔): 程序设计实验 c++
本人学院
The Student Class with Private Data fields for lab标签空格分隔 程序设计实验 c
本人学院
description
读题
my answer
the standard answer
反馈
description
The class “Student” is declared as below.Student { public: Student(); Student(int, const char*, int); void set(int, const char*, int); void print(); private: int id; char name[50]; int age; };
Please implement the class to get expected output. You don’t need to submit the main function.
If the following main function is used, your implementation should get the expected output in the sample.
int main() { Student std1, std2(123, "John Smith", 19), std3(124); std1.set(100, "Bill Gates", 55); std1.print(); std2.print(); std3.print(); return 0; }
Sample Output:
Bill Gates (100) is 55 years old.
John Smith (123) is 19 years old.
No Name (124) is 0 years old.
Hint:
Private Data fields.
Default arguments.
main.cpp
#include <iostream> #include "Student.h" using std::cin; int main() { int test; int id, nid, age; char names[10][20] = { "Aries", "Bill gates", "John Smith", "Blanton", "Blanche", "Halley", "Hamilton", "Rachel", "Jacobs", "Jan"}; cin >> test; cin >> id >> nid >> age; if (test == 0) { Student st; st.print(); } else if (test == 1) { Student st(id, names[nid], age); st.print(); } else if (test == 2) { Student st; st.set(id, names[nid], age); st.print(); } else if (test == 3) { Student st(id); st.print(); } else { Student st(id, names[nid]); st.print(); } return 0; }
读题
my answer
Student.h#ifndef STUDENT_H #define STUDENT_H #include<iostream> using namespace std; class Student { public: Student(int = 0, const char* = "No Name", int = 0); void set(int, const char*, int); void print(); private: int id; char name[50]; int age; }; Student::Student(int i, const char* n, int a) { id = i; age = a; int j; for (j = 0; *n != '\0'; j++) { name[j] = *(n++); } name[j] = '\0'; } void Student::set(int i, const char* n, int a) { id = i; age = a; int j; for (j = 0; *n != '\0'; j++) { name[j] = *(n++); } name[j] = '\0'; } void Student::print() { cout << name << " (" << id << ")" << " is " << age << " years old." <<endl; } #endif
the standard answer
Student.h#ifndef _STUDENT_H #define _STUDENT_H #include <iostream> #include <string> #include <cstring> #include <cstdio> class Student { public: Student(); Student(int, const char*, int); void set(int id_, const char* name_, int age_); void print(); private: int id; char name[50]; int age; }; Student::Student() { id = 0; snprintf(name, sizeof("No Name"), "No Name"); age = 0; } Student::Student(int id_, const char* name_ = "No Name", int age_ = 0) { id = id_; snprintf(name, sizeof(name), name_); age = age_; } void Student::set(int id_, const char* name_, int age_) { id = id_; snprintf(name, sizeof(name), name_); age = age_; } void Student::print() { printf("%s (%d) is %d years old.\n", name, id, age); } #endif
反馈
snprintf()函数将const char * 转换为 string
第一个参数为目的string的变量名
第二个参数为要复制的长度
第三个参数为源const char *的变量名
相关文章推荐
- 1348 [BA1000] The Student Class with Public Data Fields
- StructLayoutAttribute- control the physical layout of the data fields of a class or structure.
- Working with Data in ASP.NET 2.0 :: Using TemplateFields in the GridView Control
- SQL Transactions with the SqlTransaction Class (ZT)
- use stack to initial a treeview with the data in a database
- ASP.NET操作EXCEL时出现的错误 Retrieving the COM class factory for component with CLSID(转)
- Get Started with the Enterprise Library Data Access Application Block
- Complexities with NHibernate, DataTransferObjects and the Assembler Pattern(Ben Scott's Blog)
- Grab a SQL Image data type with the Connector, ODBC or Microsofts ADO
- Lesson 04: Reading Data with the SqlDataReader(使用SqlDataReader读取数据)
- the differences of DataRelation class between 1.1 and 2.0
- How to: Send Data Using the WebRequest Class
- How to solve the problem "A project with an Output Type of Class Library cannot be started directly "
- Implementing Virtual Mode with Just-In-Time Data Loading in the Windows Forms DataGridView Control
- Lesson 05: Working with Disconnected Data - The DataSet and SqlDataAdapter(使用断开数据--DataSet和SqlDataAdapter)
- Encrypting Data With the SQL Server Encrypt Function
- What's the deal with Databinder.Eval and Container.DataItem?
- 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).
- Using ASP.NET 3.5's ListView and DataPager Controls: Displaying Data with the ListView (翻译)