Need Help With Private/Public Class

Ok so I am making a class "Box" with private variables and I have made public member functions to be able to populate the private variables right? So my question is can i use the "::" scope operator for my public member functions outside of my class to populate my private variables if i have prototyped the member functions inside of class Box?

closed account (D80DSL3A)
You use the :: operator when defining the function, and the . operator when calling it.
Example:
1
2
3
4
5
6
7
8
9
10
// definition
void Box::setWidth( int Width )
{
    width = Width;// width being private
}


// use
Box b;
b.setWidth(5);


Hope that helps.
Last edited on
Hi,

Also investigate the use of initialiser lists to set values when the object is constructed.

http://www.cplusplus.com/doc/tutorial/classes/


in the section Member initialization in constructors

One should only need set functions if the value of member variables need to change after the object is created. Generally there should be some checking to see that the new values or the parameters supplied make sense, or that making changes are authorised, as in setBankBalance.

If the class consists of just public get and set functions along with corresponding private variables and no other functions, then it might not need to be a class at all. It could be a C style struct (public by default) which in turn could be a private member of a class that needs it.
Thanks. I have it set up like you do fun2code but my problem is i am having the user populate the box variables, so what would be the code to get the user to populate it. For example would i use say,

void Box::setWidth( int Width )
{
width = Width;// width being private
}


Box b;
int boxwidth;

cout << "what do you want the width of the box to be?";

cin >> boxwidth;

b.setWidth(boxwidth);

would this work to have the user to populate the width value?
closed account (D80DSL3A)
That should work. You can test it by writing your getWidth() function, then:
cout << "box width now = " << b.getWidth();
You could get all the info first, then create the object with the appropriate constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
unsigned int boxwidth = 0;

std::cout << "what do you want the width of the box to be?";

std::cin >> boxwidth;
// make sure to handle input errors & validate the input

unsigned int boxheight = 0;

std::cout << "what do you want the height of the box to be?";

std::cin >> boxheight;
// make sure to handle input errors & validate the input

// if input is OK and validated, create the object
Box b(boxwidth, boxheight);

std::cout << "box width now = " << b.getWidth() << "\n";
std::cout << "box height now = " << b.getHeight() << "\n";


Of course if you want to change the values again, then use the set functions
thanks guys. good stuff. Got it working now. Also, can I create all the objects at once after getting a bunch of info like this? Or should i do it after getting each bit of info? (this is for a car inventory where i'm populating public member functions for private variables by the way)

car[ctr].SetMake(userMake);
car[ctr].SetYear(userYear);
car[ctr].SetModel(userModel);
car[ctr].SetMileage(userMileage);
car[ctr].SetName(userName);
Ok, still no need for set functions: Get the info, make a car object with it, assign to an element of the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "CCar.h"

int main()
{
   CCar CarInventory[10];

   CCar MyCar("Holden", 2014, "Commodore", 10000, "Ginger"); // I wish

   CarInventory[0] = MyCar;

   std::cout << "MyCar is a " << CarInventory[0].getMake() << "\n";


   return 0;
}


Of course you will have a loop that collects data, validates it, makes an object, then assigns it to the array.

A better approach is to use a std::vector (if you are allowed) where you can use push_back function. You would make a vector of your class objects:

std::vector<CCar> CarInventory;

CCar MyCar("Holden", 2014, "Commodore", 10000, "Ginger"); // I wish

CarInventory.push_back(MyCar);


And you can still access them just like an array.

http://www.cplusplus.com/reference/vector/vector/push_back/
Last edited on
Topic archived. No new replies allowed.