我試圖找出類是如何工作的,但我有一點麻煩顯示類變量。在printf函數中使用字符串作爲參數。
的main.cpp
#include <stdio.h>
#include "Student.h"
#include <stdlib.h>
void main()
{
Student students;
students.Print();
system("pause");
}
Student.h
#pragma once
#include <string.h>
#include <iostream>
using namespace std;
class Student
{
public:
Student(void);
~Student(void);
void Print(void);
private:
int IDone;
int IDtwo;
string studentOne;
string studentTwo;
};
Student.cpp
#include "Student.h"
Student::Student(void)
{
studentOne = "John Doe";
studentTwo = "Jane Doe";
IDone = 227768;
IDtwo = 227769;
}
Student::~Student(void)
{
}
void Student::Print(void)
{
printf("Student name: %s\n", studentOne);
printf("Student ID: %d\n", IDone);
printf("Student name: %s\n", studentTwo);
printf("Student ID: %d\n", IDtwo);
}
當這個運行時,我得到:
Student name: <null>
Student ID: 227768
Student name: <null>
Student ID: 227769
後來我希望能夠更改名稱和ID。另外,可以將這些成員放在一個數組中,這樣我就可以通過學生[0]和學生[1]打印它們了?
添加一個家庭作業標籤。如果不適用,請刪除它。 – 2012-08-07 08:11:08
究竟是什麼問題? – 2012-08-07 08:11:13
一個小方面的說明:「學生」班的設計似乎......錯了。主要是因爲它可能應該被命名爲「學生」,因爲它包含關於兩名學生的信息。 「學生」(單數)意味着一個學生。 – 2012-08-07 08:14:40