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::stringand you will find a method namedc_strthat is used to get a C-style character pointer usable in e.g.printf.Or just start using
std::coutinstead: