Help with classes?

Pages: 12
cire, i'm having the same problem with a similar assignment. perhaps you could be more specific as to what you mean by defining the methods?

Thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//this is class definition
class Some
{
  public:
    //this is methods declaration
    void set();
    void get();
    //end of method declaration
};
//end of class definition

//this is methods definition
void Some::set()
{
  std::cout << "Hi" << std::endl;
}

void Some::get()
{
  std::cout << "I will get you!" << std::endl;
}
//end of method definition 
thanks Shinigami. that helps a lot. but what if you're not just trying to output text? for instance, how would you initialize an array in the definition?
Initializing an array or something of the sort in the definition of a class? That would involve constructors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Array
{
  public:
    Array();
  private:
    int array[10];
};

Array::Array()
{
  for (int i = 0; i < 10; i++)
  {
    array[i] = i;
  }
}

or
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Array
{
  public:
    Array();
  private:
    int array[10];
    void make_array();
};

Array::Array()
{
  make_array();
}

void Array::make_array()
{
  for (int i = 0; i < 10; i++)
  {
    array[i] = i;
  }
}
Last edited on
Thanks for all of your help.
I ended up figuring it out:
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
.h file:

#include <string>
using namespace std;
#ifndef bstore_H
#define bstore_H

class bstore
{
public:
	bstore();
	bstore(string);
	string title();
	string author();
	string price();
	
	
private:
	
	string name;
	string money;

	
};
#endif 

first cpp file:
#include "hw.h"
#include <string>
#include <iostream>
using namespace std;

bstore::bstore(){
}

string bstore::title()
{
	cout<<"Title of book: "<<endl;
	cin>> name;
	return name;
}
string bstore::author()
{ 
	cout<<"Name of Author: "<<endl;
	cin>> name;
	return name;
}
string bstore::price()
{ 
	cout<<"Cost of Book: "<<endl;
	cin>> money;
	return money;
}



main code:

#include "hw.h"
#include <string>
#include <iostream>
using namespace std;
string bookinfo[8][3];
string k;
int l;
string changes;
int main()
{
	bstore basicinfo;
		
	string basicinfotitle;
	string basicinfoauthor;
	string basicinfoprice;

	int j;
	for(j=0;j<7;j++){
		cout<<"book number "<<j<<endl;
	basicinfotitle=basicinfo.title();
	basicinfoauthor=basicinfo.author();
	basicinfoprice=basicinfo.price();
	
	bookinfo[j][0]=basicinfotitle;
	bookinfo[j][1]=basicinfoauthor;
	bookinfo[j][2]=basicinfoprice;
	
}
	string yesno;
	
	while(yesno!="q"){
cout<<"Type display to display a table of books. Type edit to edit a value."<<endl;	
cout<<"Enter q to quit at any time"<<endl;
cin>>yesno;

if(yesno=="display"){
	cout<<"book number"<<"\ttitle"<<"\tauthor"<<"\tprice"<<endl;
	cout<<" "<<endl;
	for(j=0;j<7;j++){
	cout<<j<<"\t\t"<<bookinfo[j][0]<<"\t"<<bookinfo[j][1]<<"\t"<<bookinfo[j][2]<<endl;
	
	}
}

if(yesno=="edit"){
cout<<"Which book would you like to edit?" <<endl;
cin>>j;
cout<<"Which part would you like to edit?(Enter T,A or M)"<<endl;
cin>>k;
if(k=="T"){
l=0;
}
else if(k=="A"){
l=1;
}
else if(k=="P"){
l=2;
}

cout<<"Make changes: "<<endl;
cin>> basicinfotitle;
bookinfo[j][l]=basicinfotitle;
cout<<bookinfo[j][l];
	}
	}

}

(Sorry, I put all three documents into the same code block above but you should be able to figure it out . one is a .h one is first .cpp file and the last main code )
Topic archived. No new replies allowed.
Pages: 12