C ++ Primer study note _ 2 _ Quick Start (continued)

Source: Internet
Author: User

P15 exercises

// Question 1.14: try to analyze the output result of this program If v1 = v2 # include <iostream> int main () {std :: cout <"Enter two numbers:" <std: endl; int v1, v2; std: cin> v1> v2; int lower, upper; if (v1 <= v2) {lower = v1; upper = v2;} else {lower = v2; upper = v1;} int sum = 0; for (int I = lower; I <= upper; ++ I) {sum + = I;} std :: cout <"Sum of" <lower <"to" <upper <"Aggressive is" <sum <std: endl; return 0 ;}

// 1.16 # include <iostream> int main () {std: cout <"Please input a sequence of numbers:" <std: endl; int val; int count = 0; // For the judgment condition, first perform the input operation while (std: cin >>> val) {if (val <0) {++ count ;}} std: cout <"There is" <count <"negatives" <std: endl; return 0 ;}

// Question 1.19 # include <iostream> int main () {std: cout <"Please input two numbers:" <std: endl; int v1, v2; int count = 1; std: cin> v1> v2; for (int I = v1; I <= v2; ++ I) {std :: cout <I <''; if (count % 10 = 0) {std: cout <std: endl ;}++ count ;}return 0 ;}

V. Introduction to Classes

1. In C ++, we define a class to define our ownData Structure

Actually,C ++The main focus of the design is to make the behavior of the defined class type as natural as the built-in type !!!


2. When using a class, we do not need to specify how this class is implemented. On the contrary, we need to know what operations this class can provide!


3. For custom classes, the compiler must be able to access class-related definitions.


4. Generally, the file name is the same as the class name defined in the header file. Usually the suffix is. h, but some ides will pick up the suffix of header files!


5. When using a custom header file, we use double quotation marks (") to include the header file.


6. When calling a member function, (usually) Specify the object to be operated by the function. The syntax is to use the vertex operator (". "), the left operator must be of the class type, and the right operator must specify members of this type.


P19

// Exercise 1.21 # include <iostream> # include <fstream> # include "Sales_item.h" int main () {freopen ("book_sales", "r", stdin); Sales_item item; while (std: cin> item) {std: cout <item <std: endl;} return 0 ;}

// Exercise 1.22 # include <iostream> # include <fstream> # include "Sales_item.h" int main () {freopen ("book_sales", "r", stdin); Sales_item item1; sales_item item2; while (std: cin> item1> item2) {if (item1.same _ isbn (item2) {std: cout <item1 + item2 <std:: endl ;}} return 0 ;}

6. C ++ Program

#include <iostream>#include <fstream>#include "Sales_item.h"int main(){    //freopen("book_sales.","r",stdin);    Sales_item total,trans;    if (std::cin >> total)    {        while (std::cin >> trans)        {            if (total.same_isbn(trans))            {                total += trans;            }            else            {                std::cout << total << std::endl;                total = trans;            }        }        std::cout << total << std::endl;    }    else    {        std::cout << "No Data?!" << std::endl;        return -1;    }    return 0;}

[Note: The Sales_item class needs to be downloaded and referenced on the Turing Website: www.turingbook.com]

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.