Linux to write C + + instance program (four) __ Storage

Source: Internet
Author: User
Tags class definition function definition function prototype

Instance 1

Topic: Define a Dog class that contains attributes such as Age,weight, as well as methods for manipulating these properties. Implement and Test this class.

Description: This is my textbook after the class, mainly for the practice of class, the content is written by themselves, you casually write on the line.

SOURCE program:

Program execution Results

After pressing any key

3 seconds later

Example 2:

Topic: Define a Circle class, have the data member radius (RADIUS), member function Getarea (), compute the area of the circle, construct a circle object to test

Analysis: Textbook After class, I wrote a very simple small program

Program execution Results

Instance 3

Writing a program is familiar to the scope. The global variables declared in this example have namespace scopes that are valid throughout the file.

Knowledge points: Mainly to test the scope of the wipe.

SOURCE program:

Execution results:

Instance 4

Write programs to learn about the lifetime and visibility of variables.

Knowledge Point: The lifetime of an object can be divided into static lifetime and dynamic lifetime. The rest of the objects have dynamic lifetimes in addition to the objects declared in the namespace scope and those that are decorated with the keyword static in the local scope.

SOURCE program:

Program execution Results:

Instance 5

A clock program with static and dynamic lifetime objects.

Knowledge Point: Here still takes the clock class as an example, in this instance, declared has the function prototype scope, the local scope, the class scope and the namespace scope multiple objects

Program execution Results:

Analysis: In this program, variables and objects with various scope types are included. The 3 parameters of the function member SetTime in the Clock class definition have the function prototype scope, the SetTime function definition 3 parameter, the object Myclock has the local scope, the clock class data, the function member has the class scope, the object Globclock has the namespace scope. In the main function, these variables, objects, and the public members of the object are visible. In terms of lifetime, except for objects with a namespace scope, Globclock has a static lifetime, and the rest has a dynamic lifetime, as in the case of the program's runtime.

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.