I am trying to create a following code with the information below.

How would this be written with the following information:


Create a Person class. This class has the following private members:
char name[40];
char lastname[40];
int ID;

Write a default constructor that initializes the name and lastname to “UNKNOWN” and ID to -1;
Write a constructor that takes, a string for name, a string for last name and an int for ID;
Write a public member method called display () that prints out the data of a person (name, last name and ID).
In your main (), create two objects of type Person, one using the default constructor and one initialized to your name, last name and an arbitrary ID. Call the display() method for both objects.
Last edited on
I am trying to create the following with the information below

Go ahead then. You have my permission.
Last edited on
Just do each thing, one at a time.
It's telling you to write a class and it already gives you exactly what properties you need to put in the body of the class. If you don't know how to make a class and a constructor, read any C++ class tutorial.
http://www.cplusplus.com/doc/tutorial/classes/
Last edited on

#include <iostream>
#include <string.h>

using namespace std;

class Person {
private:
int ID;
char name [40];
char lastname[40];
public:
Person() {
ID = -1;
strncpy(name, "UNKNOWN", sizeof(name));
strncpy(lastname, "UNKNOWN", sizeof(lastname));
}
void display(){
cout<<"ID = "<<ID<<endl;
cout<<"name = UNKNOWN "<<endl;
cout<<"lastname = UNKNOWN "<<endl;
}
};
int main(){
Person obj;
obj.display();
return 0;

}
Topic archived. No new replies allowed.