Classes for C + +

Source: Internet
Author: User


The class that created C + + has two files: two files ending with. cpp and. h End with a header file, an externally exposed interface. C.PP is a concrete implementation. That is, declarations of variables and functions, or declarations of classes, are written in the. h header file.


? Defining classes with classmember defaults to privatethe scope of a member in a classPrivateprotected Public


The following is a simple creation of a simulated TimeTime Class
The header files are as follows:
  time.h//  classandobject////  Created by, on 15/1/19.//  Copyright (c) 2015 Huangyongrui. All rights reserved.//#ifndef __classandobject__time__#define __classandobject__time__#include <iostream> Using namespace std;//class Time {    //three Private member variable time division seconds    Int hour;    int min;    int sec;    Public:    //The declaration of the constructor method    //.h file can give the parameter default value, but be aware of the initialization list and implementation part in the CPP file time    (int h = 0,int m = 0,int s = 0);        Run    void Run ();    private://private    void Dida ();    void Show ();}; #endif/* Defined (__classandobject__time__) */


the. cpp files that are specifically implemented are as follows:
////time.cpp//classandobject////Created by 15/1/19.//Copyright (c) 2015 Huangyongrui. A ll rights reserved.//#include "Time.h"//note in the implementation of the function part of the parameter cannot appear by default//which class under which method//initialization list time::time (int h,int m,int s): Ho ur (h), Min (M), SEC (s) {cout << "" << Endl;} This is a global function that is not a method under the time class so you cannot get member variables/*void Show () {cout << hour << "when" << min << "minutes" << SEC << "SEC" << Endl;} *///which class is under which function void Time::show () {cout << hour << "when" << min << "Minute" << sec << "SEC" < ;< Endl;}    void Time::d Ida () {sec++;        if (= = sec) {sec = 0;    min++;        } if (= = min) {min = 0;    hour++;    } if (hour) {hour = 0;        }}//run void Time::run () {while (1) {Show ();        Dida ();        Represents a time time_t cur = time (0);//0 is the moment to get the current system//pay attention to understanding while (cur = = times (0)) {//Dead loop 1 seconds }    }}

The above is a simple class, and then you need to import the header file when other files are created to create the class object.
#include <iostream> #include "Time.h"  //double quotation marks are imported by their own class using namespace std;//main function int main () {    // You need to create a time object time    t (21,18,32);    T.show ();//private method access not to    //t.dida ();    T.run ();        return 0;}

The results of the operation are as follows:
21:18 32 sec 21:18 33 sec 21:18 34 sec. 21:18 35 sec. 21:18 36 sec program ended with exit Code:9



Classes for 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.