Detailed description of static data members in C + +

Source: Internet
Author: User

This article and everyone to share is mainly in C + + static data members of the relevant usage and source code examples, hoping to help you better learn C + +.

  Static(statically stored) data member

  StaticTest.cpp: defines the entry point of the console application.

  1. Knowledge points

  Static(statically stored) data member: created and initialized at compile time.

  2. Code

  #include "stdafx.h"

  #include

  using namespace Std;

  Class computer

  {

  Private

  float price;

  Public

  static float total_price;//static data member to compiler description: How to allocate memory for static data members

  Computer (const float p)

  {

  Price = P;

  Total_price + = p;

  }

  ~computer ()

  {

  Total_price-= Price;

  }

  void print ()

  {

  cout<< " Total Price:" <<total_price<<endl;

  }

  };

  float Computer::total_price = 0;// true memory allocation

  int _tmain (int argc, _tchar* argv[])

  {

  Computer COMP1 (7000);

  cout<< " buy computer 1 " <<endl;

  Comp1.print ();

  cout<<computer::total_price<<endl;;

  Computer COMP2 (4999);//

  cout<< " Total Price:" << " after purchasing computer 2 " <<endl;

  Comp1.print ();

  Computer Comp3 (2500);

  cout<< " buy computer 3 " <<endl;

  Comp1.print ();

  push off the computer 2

  Comp2.~computer ();

  cout<< " back computer 2 " <<endl;

  Comp1.print ();

  return 0;

  }

  3. Running Results

original link:http://www.maiziedu.com/wiki/cplus/data/

Detailed description of static data members in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.