question with classes

in this program i have a class set up to gather a craigslist style post from a user and save all the information to different arrays within the class (which is also an array, to hold multiple posts in the same run) and i have another class to search the previous class and find specific types of posts.

my question is how to reference one classes private members while in the other class. i assumed it was as easy as doing class_var.member but im getting alot of weird errors and i havent been able to figure out how to do it correctly.
any help/advice is greatly appreciated

my .h
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
#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

class Posting            //class
{
public:
	Posting();       //constructor
	~Posting();          //destructor

	void Create_Posting(char again, bool &check);  //create a new posting of this title.
	void display_post();
	bool is_type();  //is the posting a job, housing, item for sale, or free stuff

private:
	float cost;
	char *description;
	char *title;
	char *type;
	float sqfeet;
	float rooms;
	float rent;
	char *location;
	char *email;
	int size;	
	float pay;
	float bathrooms;
};

class list
{
public:
	list();
	~list();

	void Add(char title[]);
	void display_all();
	void display_type(char type[]); //only display those postings that match a type

private:	
	char *search_type;
							//put the information about an array of postings
	int count;				//and an integer count of the number of postings available
	int size;
};

my main
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
//sean kemper
//assignment 4

#include "main.h"
using namespace std;


int main()
{
	Posting post[50];  // able to hold 50 posts 
	list list;
	char again('Y');
	bool check = false;
	int menu_choice(0);

	

	//cout << "would you like to create a post?(y/n): " << endl;
	//cin >> again;

	while(again =='y' || again == 'Y')
	{
		cout << "---Main Menu---" << endl;
		cout << "1) Create a post\n"
			 << "2) Search for a type of post\n"
			 << "3) Display all posts\n"
			 << "4) Total number of posts'\n'";

		cin >> menu_choice;
		cout << '\n';

		switch (menu_choice)
		{
		case 1:
			//create a new post

			for( int i(0); i < 50; i++)        //for loop to save more than 1 post per run
			{
				check = post[i].is_type();  // setting check equal to the bool returned by is_type
				if(check == true)
					post[i].Create_Posting(again , check);    //if check was true then we can run create_posting

				if(check == false)
					cout << "sorry that is an incorrect post type" << endl;   //otherwise we have to prompt for another post type

				cout << "\n	would you like to return to the main menu? " << endl;
				cin >> again;
				toupper (again);
			}

		case 2:
			//search for a specific type of post
			for(int i(0);i < list.count;i++)
			list.display_type(post[i].type);

		case 3:
			//display all posts
			list.display_all();

		case 4:
			//total number of posts
			cout << "There are " << list.count << "Total posts archived" << endl;
			return 0;
		}
	}
}


my search class
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
#include "main.h"
using namespace std;

list::list()
{
	int size = 20;
	int count = 0;
	search_type = new char[size];
}
list::~list()
{
	delete [] search_type;
}
void list::display_type(char type[],)
{
	Posting post;

	if (search_type)
		delete [] search_type;

	cout << "which type of post doy ou want to search for? (job, housing, sale, free): " << endl;
	cin.getline(search_type, 20);
	cin.ignore(20, '\n');

	for (int i(0); i < list.count; i++)
	{

		if( strcmp (post[i].type, search_type) == 0)
		{
			cout << post[i].type << endl;
			cout << post[i].title << endl;         //This is where most of my errors stem from. referencing the variables that are in the private sector of my post class
			cout << post[i].description << endl;
			cout << post[i].email << endl;
			if (strcmp (search_type, "job") == 0)
			{
				cout << "Wage: $" << post[i].pay << "Per hour " << endl;
				cout << "Location at: " << post[i].location << endl;
			}
			if(strcmp (type, "housing") == 0)
			{
				cout << "Square feet: " << post[i].sqfeet << endl;
				cout << "Bedrooms: " << post[i].rooms <<endl;
				cout << "Bathrooms: " << post[i].bathrooms << endl;
				cout << "Rent: $" << post[i].rent << endl;
				cout << "Address: " << post[i].location << endl;
			}
			if (strcmp (type, "sale") == 0)
			{
				cout << "$" << post[i].cost << endl;
			}
		}
	}
}

void list::display_all()
{

	cout << post[i].type << endl;
	cout << post[i].title << endl;
	cout << post[i].description << endl;
	cout << post[i].email << endl;

	if (strcmp (search_type, "job") == 0)
	{
		cout << "Wage: $" << post[i].pay << "Per hour " << endl;
		cout << "Location at: " << post[i].location << endl;
	}
	if(strcmp (type, "housing") == 0)
	{
		cout << "Square feet: " << post[i].sqfeet << endl;
		cout << "Bedrooms: " << post[i].rooms <<endl;
		cout << "Bathrooms: " post[i].bathrooms << endl;
		cout << "Rent: $" << post[i].rent << endl;
		cout << "Address: " << post[i].location << endl;
	}

	if (strcmp (type, "sale") == 0)
	{
		cout << "$" << post[i].cost << endl;
	}
}



i left out my post.cpp because its rather long and there's really no errors with it. if you need to see the code to be able to help find the source of the problem i can post it
Last edited on
reference one classes private members while in the other class

In the class that has the members you can declare the class that wants members as a friend. Or you can write accessor and mutator methods for the fields you want to access from outside the class.
this is my first program using classes, and im not really sure what you mean. sorry, im pretty new to c++. how do y ou declare it as a "friend"?
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
#include <iostream>

//don't forget to forward-declare this, 
//otherwise PrivateFellow won't recognize the class
class CreeperFriend;

class PrivateFellow
{
public:
    PrivateFellow()
    {
        myLittleSecret = 12;
    }
    
    //here we declare the other class is a friend
    friend class CreeperFriend;
private:
    int myLittleSecret;

};

class CreeperFriend
{
public: 
    void ExposeSecret(PrivateFellow pf)
    {
        //right here we access a private field of another class!
        std::cout << "Hey! This guy's secret is " << pf.myLittleSecret << std::endl;
    }
};

int main()
{
    PrivateFellow secretAgent;
    CreeperFriend loudmouth;
    loudmouth.ExposeSecret(secretAgent);
    return 0;
}


A little more explanation here: http://www.cplusplus.com/doc/tutorial/inheritance/
Last edited on
ok. sweet. do i make the friend declaration in both classes private sections or does it only need to be stated once
Only declare in both if you want the private access to work both ways. In the example I gave, CreeperFriend can see PrivateFellow's private members, but not the other way around.

If you had them both declare each other as friends, then private access would be a two-way street.

ALL THAT BEING SAID: this is not a situation where you would typically use friend classes. Friend classes violate encapsulation, one of the pillars of object-oriented programming.
More likely you would create a public function in PrivateFellow called getMyLittleSecret which would return the private field. That way, people who wanted to know the secret would call getMyLittleSecret. In this way, you have better control over how your private variable is accessed by outsiders.
Last edited on
hmm ok. so generally don't use friend classes as it pretty much defeats the whole purpose of a class(that nobody else can modify the data).

well declaring them as friends definitely made the majority of the errors i was getting stop
Here's what I mean:
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
#include <iostream>

//commented this out... forward declare not needed anymore anyway
//class CreeperFriend;

class PrivateFellow
{
public:
    PrivateFellow()
    {
        myLittleSecret = 12;
        timesIToldMySecret = 0;
    }

    int tellMySecret()
    {
        timesIToldMySecret++;
        return myLittleSecret;
    }

    //Commented out! As far as the program is concerned, these classes aren't friends
    //friend class CreeperFriend;
private:
    int myLittleSecret;
    int timesIToldMySecret;

};

class CreeperFriend
{
public: 
    void ExposeSecret(PrivateFellow pf)
    {
        //this way wouldn't work anymore because they are not friends
        //std::cout << "Hey! This guy's secret is " << pf.myLittleSecret << std::endl;

        //use the public method instead!
        std::cout << "Hey! This guy's secret is " << pf.tellMySecret() << std::endl;
    }
};

int main(int argc, char* argv)
{
    PrivateFellow secretAgent;
    CreeperFriend loudmouth;
    loudmouth.ExposeSecret(secretAgent);
    return 0;
}


When I say control over access, notice how behind the scenes, PrivateFellow is incrementing a count of how many times he's told his secret. Maybe later he'll use this information to see if more people know his secret than he told.
Last edited on
haha im really enjoying your example. depicts the idea's behind private and public and friends well!
and coming from boo radley who's a rather private fellow
I hate examples that use names that don't really feel like they mean anything, like 'int i;' or 'class MyClass'. They don't feel tangible and they make the code read less naturally. Naming classes, methods, and variables with descriptive names is the biggest favor you can do for yourself, especially if you're going to revisit the code a few weeks or months from now. Also, comments and whitespace are free! I use them to make my program more pleasant to read.
Topic archived. No new replies allowed.