简体   繁体   中英

Displaying class variables. Using string as parameter in printf function.

I'm trying to figure out how classes work, but I'm having a bit of trouble

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);

}

When this runs I get:

    Student name: <null>
    Student ID: 227768
    Student name: <null>
    Student ID: 227769

Later I want to be able to change the names and IDs. Also, it is possible to have these member in a type of array so I could print it by going student[0] and student[1]?

Read a reference about std::string and you will find a method named c_str that is used to get a C-style character pointer usable in eg printf .

Or just start using std::cout instead:

void Student::Print(void)
{
    std::cout << "Student name: " << studentOne << '\n';
    std::cout << "Student ID: " << IDone << '\n';
    std::cout << "Student name: " << studentTwo << '\n';
    std::cout << "Student ID: " << IDtwo << '\n';
}

You need to use stl containers like vector instead of arrays to store your student detail and you should remodel your student class to something like this

class Student
{
private:
 string Name;
 int    Id;
public:
 Student(string name, int id);
 string GetName();
 void SetName(name);
 int GetId();
 void SetId(int id);
 void Print();
};

and your main should be like this

   void main()
    {
      vector<Student> studentList;

      Studen one("John Doe", 1);
      Studen two("Jane Doe", 2);

      studentList.push_back(one);
      studentList.push_back(two);

      vector<Student>::const_iterator cii;
      for(cii=Student.begin(); cii!=Student.end(); cii++)
       {
          *cii.Print();

       }

    }
printf("Student name: %s\n", studentOne);

Results in undefined behavior. You need to pass ac style string which suits the %s format specifier, ie studentOne.c_str() .

it is possible to have these member in a type of array so I could print it by going student[0] and student[1]

You can always declare an array of std::string as any other data type.

class Student
{
  ...
  string students[N];  // or vector<string> students;
};
printf("Student name: %s\n", studentOne);

You can't pass a C++ std::string to the C printf function; you can only pass the basic types that are shared with the C language; passing a class type will cause undefined behaviour.

You could use C++ output:

std::cout << "Student name: " << studentOne << std::endl;

or, if you really want to use C ouput for some reason, you could extract a C-style string from the std::string :

printf("Student name: %s\n", studentOne.c_str());
                                       ^^^^^^^^

Also, it is possible to have these member in a type of array so I could print it by going student[0] and student[1]?

Yes, you can put most types, including classes, in an array:

std::string student[2]; // array of two strings.

If you want a variable number, use a dynamic array:

std::vector<std::string> student;

It seems a bit odd for a single Student to have two names and two IDs, though. Shouldn't that class just represent one person?

Also, it is possible to have these member in a type of array so I could print it by going student[0] and student[1]?

Sure. If you know number of students upfront, then you can just:

string students[2];

This statement will effectively allocate two string objects in memory and you can reference each of them by array index.

If you dont know how many students you would need, then the best is to use std::vector class, because it is preferred way of representing dynamic arrays in C++.

You placed studentOne, studentTwo, IDOne and IDTwo data members under private section of your class declaration. This means you are allowed to manipulate them directly only within Student class implementation. The approach that most of other programmers would expect you to follow is to create special getter and setter methods:

const std::string& studentOne() const {return studentOne;}
void setStudentOne(const std::string& aStudentOne) {studentOne = aStudentOne;}

and so on. Creating setStudentOne is especially useful when you want to have some side effect. Otherwise, you can have single method to access it:

std::string& getStudentOne() {return studentOne;}

Alternatively, exposing direct access to fields (ie relocating them to under public section) is not a taboo . Just remember, this is not what most people would expect to see.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM