OOP: Class (object) data type 'string'

I haven't been able to find a video or 'tut' that showcases what I'm trying to accomplish, which is this:

Class must have a constructor.

Class must have 2 class member variables of different data types, accessors, and mutators (getters and setters) to access these variables.

Program must have 3 files, one header file, one .cpp file for that class and one Main.cpp.

So far, I have been able to successfully create a class that meets the requirements for one data type, 'int'. The problem I am running into is, for my second data type, I opted to use a 'string' data type variable. (I realized I could just have easily used a 'char' data type as the other member variable but, I thought this would be more challenging. So far, it apparently has been as I'm stumped cold)

I could really use some help from anyone looking from outside the bubble :) Any pointers would be appreciated, tremendously. As always, thank you for your time and patience with a newbie.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 // HEADER FILE: type.h //

class type
{
private:

	string myBreed;
	string breed;
		
public:

	type(void);
	~type(void);
	
	void getBreed();
	void setBreed(string);

}; // end of HEADER FILE: type.h //

// IMPLEMENTATION FILE: type.cpp //

 #include <string>
#include "type.h"

using namespace std;

type::type(void)
{
	cout << "creating breed" << endl;
}


type::~type(void)
{
}

void type::getBreed()
{
	return myBreed;
}

void type::setBreed(string myBreed)
{
	breed = myBreed;

} // end ofIMPLEMENTATION FILE: type.cpp //

// MAIN FILE //

#include <iostream>
#include "cat.h"
#include "type.h"

using namespace std;

int main()
{

	cat myCat;
	myCat.setAge(10);

	type mBreed;
	myBreed.setBreed("calico");

	cout << myCat.getAge() << endl;
	
	cout << myBreed.getBreed() << endl;

system("pause");
return 0;

} // end of MAIN FILE // 
Last edited on
Is the tutorial wanting you to write your own "string" class, without using the standard C++ classes?

(I assume "OOP: Class (object) data type 'string'" is the title of the exercise.)

Writing a string class without std::string will be more challenging, as you'll have to do everything the standard class usually does for you! So char (or rather, char*) plus int is prob the way to go.

Andy
Thanks, Andy, I am slowly coming to that conclusion :)
> I haven't been able to find a video or 'tut' that showcases what I'm trying to accomplish
Good, learn to generalize.

> I could really use some help
¿help with what? ¿what is your problem?

1
2
3
4
5
6
7
8
//2 class member variables of different data types
	string myBreed;
	string breed;

void type::getBreed()
{
	return myBreed; //¿?
}
Is this

OOP: Class (object) data type 'string'

Class must have a constructor.

Class must have 2 class member variables of different data types, accessors, and mutators (getters and setters) to access these variables.

Program must have 3 files, one header file, one .cpp file for that class and one Main.cpp.

the whole of the prob spec?

Andy
Topic archived. No new replies allowed.