C++私有成员

#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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


#include <iostream> using namespace std; int main () { int*ptr = NULL; cout << "The value of ptr is " << ptr ;
#include <iostream> using namespace std; int main () { intvar = 20;// actual variable declaration.
// #include <iostream> using namespace std; int main () { intvar1; char var2[10]; cout << "Address of var1 variable: ";
#include <iostream> #include <string> using namespace std; int main () { string str1 = "Hello";
#include <iostream> #include <cstring> using namespace std; int main () { char str1[10] = "Hello";
#include <iostream> using namespace std; int main () { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\\0'};
#include <iostream> #include <ctime> using namespace std; // function to generate and retrun random numbers.
#include <iostream> using namespace std; double getAverage(int arr[], int size) { int i, sum = 0; double avg;
#include <iostream> using namespace std; int main () { // an array with 5 elements. double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
#include <iostream> using namespace std; int main () { // an array with 5 rows and 2 columns. int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
#include <iostream> using namespace std; #include <iomanip> using std::setw; int main () { int n[ 10 ];// n is an array of 10 integers
#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main () {
#include <iostream> #include <cmath> using namespace std; int main () { // number definition:
#include <iostream> using namespace std; int main () { // number definition: shorts; inti; longl; floatf;
#include <iostream> using namespace std; // function definition to swap the values. void swap(int &x, int &y) {
#include <iostream> using namespace std; // function definition to swap the values. void swap(int *x, int *y) {
#include <iostream> using namespace std; void swap(int x, int y) { int temp; temp = x; /* save the value of x */
#include <iostream> using namespace std; int sum(int a, int b = 20) { int result; result = a + b; return (result);
// 标准输入流 #include <iostream> using namespace std; int main() { char name[50]; cout << "Please enter your name: ";
// C++标准输出流 #include <iostream> using namespace std; int main() { char str[] = "Hello C++";