Skip to content

Instantly share code, notes, and snippets.

@Maximaximum
Created December 15, 2015 17:20
Show Gist options
  • Save Maximaximum/3ac9d10ee40f6635d999 to your computer and use it in GitHub Desktop.
Save Maximaximum/3ac9d10ee40f6635d999 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
class Box
{
public:
double length;
void setWidth( double wid );
double getWidth( void );
private:
double width;
};
// Member functions definitions
double Box::getWidth(void)
{
return width ;
}
void Box::setWidth( double wid )
{
width = wid;
}
// Main function for the program
int main( )
{
Box box;
// set box length without member function
box.length = 10.0; // OK: because length is public
cout << "Length of box : " << box.length <<endl;
// set box width without member function
// box.width = 10.0; // Error: because width is private
box.setWidth(10.0); // Use member function to set it.
cout << "Width of box : " << box.getWidth() <<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment