SUPER BEGINNER class question

What are the contents of the two Name variables after each of the following statements assuming they are executed in the order listed?

1
2
3
4
5
6
7
8
9
class Name{
public:
	string first;
	string middle;
	string last;
};

Name yourName;
Name myName;


a. yourName.first = "George";
b. yourName.last = "Smith";
c. myName=yourName;
d. myName.middle="Nathaniel";
e. yourName.middle=myName.middle.at(0) + "."

So I'm just confused on how classes work and if there is any way to check user defined class values since you can't output them to a screen with cout. Also does every class have a first middle last string and combined they form the value of the variable (the answer)?

So is this correct?
a. yourName=George
b. yourName=George Smith
c. myName=yourName=George Smith
d. myName=George Nathaniel Smith yourName=^
e. myName=^ yourName=George N. Smith

or do you represent it as an array or what I'm confused

Thanks!
Last edited on
since you can't output them to a screen with cout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
using namespace std;

class Name{
public:
	string first;
	string middle;
	string last;
};




int main()
{
	Name yourName;
	Name myName;
	yourName.first = "George";
	yourName.last = "Smith";
	myName = yourName;
	myName.middle = "Nathaniel";
	yourName.middle = myName.middle.at(0) + ".";
	cout << "yourName first " << yourName.first << endl;
	cout << "YourName middle " << yourName.middle << endl;
	cout << "yourName last " << yourName.last << endl;

	cout << "myName first " << myName.first << endl;
	cout << "myName middle " << myName.middle << endl;
	cout << "myName last " << myName.last << endl;

	cin.ignore();
	return 0;
}
Your output is correct. Well... except for the yourName=^ and myName=^ parts. I'm not sure what that's supposed to be.

Classes are representations of things. Almost anything can be represented by a class. A book for instance contains pages, words, chapters... and classes also allow you to define functions that can be performed on that data. You can turn the page, count the words, skip to a chapter...

When you create an instance of a class Name yourName; in your code, you are creating a space in memory to store all of the information about that specific Name. You then go on to create another name Name myName; which is a second independent instance of the class. The two instances are not related to each other in any way, and do not exist in memory in any predictable locations relative to each other. The data members themselves exist in a stack that is constructed for the class itself, so in a sense the data is in an array, but you don't access it as such. In fact, it would be a bit dangerous to assume you can find the middle name by using a memory offset from the first name unless you really know how your data is being stored in memory. I wouldn't suggest trying it.

In your case, the class is very primitive, and is more closely related to a struct which is a container for storing variables in named instances. The real power of classes comes from the ability to write functions to perform actions on those variables.

ie.

1
2
3
4
5
6
7
8
class Name {
  public:
    string first;
    string middle;
    string last;

    string getMiddleInitial() { return middle.at(0)+"."; }
};


You don't explicitly store the middle initial, but you can write a function that will extract the information from the variable "middle" and return it.

Since first, middle, and last are all strings, you can print them with cout. You just need to access them through the class instance that you want to view.

ie. cout << myName.first << endl;

Since myName and yourName can be different, the values of yourName.first and myName.first can be different, so you need to tell "cout" which one you want to print. Simply writing cout << first; would be ambiguous since there would be multiple first name strings running around in your program.
Last edited on
Topic archived. No new replies allowed.