jeudi 21 janvier 2021

get method/Function giving me an unexpected number in C++

I have 3 files main.cpp, Student.cpp and Student.h The code is below:

main.cpp

#include<iostream>
#include "Student.h"
using namespace std;



int main()
{
    Student obj;
    obj.setName("My name is Uzair Khan. \n");
    cout << obj.getName();



    obj.setRollNumber(201);
    cout << obj.getRollNumber();

}

Student.cpp

#include "Student.h"

void Student::setName(string x)
{
    name = x;
}

string Student::getName()
{
    return name;
}

int Student::setRollNumber(int rollNumber)
{
    int RollNum = rollNumber;
    return RollNum;
}

int Student::getRollNumber()
{
    return rollNumber;
}

Student.h

#pragma once
#include <string>
using namespace std;



class Student
{
public:
    void setName(string x);
    string getName();
    int setRollNumber(int rollNumber);
    int getRollNumber();

private:
    string name;
    int rollNumber;
};

The output is very unexpected. I don't know why I am not able to get the roll number as clear integers, instead of getting Roll Number 201. I got something like this -858993460. I don't know why. I am new to C++. If anyone could help/explain, that would we very helpful. Thank You

(Note:- I am using Visual Studio 2019 community edition)

Output:

My name is Uzair Khan.
-858993460

enter image description here

Aucun commentaire:

Enregistrer un commentaire