Day1||C++ Primer 啃砖头书第一天

Chapter1 Getting started Notes

I/O in cpp

input see-in -> cin

output see-out -> cout

code example for i/o:

#include <iostream>
int main()
{
std::cout << "Enter two numbers:" <<std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl;
    return 0;
}

namespace:

Mechanism for putting names defined by a library into a single place. Namespaces help avoid inadvertent name clashes. The names defined by the C++ library are in the namespace std

<< : output operator

std::cout << "Enter two numbers:";
std::cout << std::endl;

first output operator prints a message to the user,second operator prints endl, which is a special value called a manipulator.Writing endl has the effect of ending the current line and flushing the buffer associated with that device.

>> : input operator

It takes an istream as its left-hand operand and an object as its right-hand operand. It reads data from the given istream and stores what was read in the given object.

(std::cin >> v1) >> v2;
//is equivalent as
std::cin >> v1;
std::cin >> v2;
//or this
std::cin >> v1 >> v2;

:: operator

Scope operator. Among other uses, the scope operator is used to access names in a namespace. For example, std::cout denotes the name cout from the namespace std.

comments

The best way to comment a block of code is to insert single-line comments at the beginning of each line

dont know how many nums

while (std::cin >> value)

. The input operator (§ 1.2, p. 8) returns its left operand, which in this case is std::cin. This condition, therefore, tests std::cin.

when test out to False?

  • An istream becomes invalid when we hit end-of-file or encounter an invalid input, such as reading a value that is not an integer. An istream that is in an invalid state will cause the condition to yield false.

How to enter an EOF?

  • CTRL + Z in windows

Class in cpp

little tips about header file:

The standard library headers typically have no suffix at all. Compilers usually don’t care about the form of header file names, but IDEs sometimes do.

Relationship between class and object:

the author of the Sales_item class defines all the actions that can be performed by objects of this class.

Definion of a object

[class] [objectName]

Member function (methods):

item1.isbn()
  • calls the isbn function that is a member of the object named item1. This function returns the ISBN stored in item1.

Summary

The most fundamental facility in C++: the class.

Its harder and more tedious than i have expected.

It just getting started 😃


Chapter2 Variables and Basic Types

Unlike py:

C++ is a statically typed language; type checking is done at compile time.

A major goal:

A major design goal of C++ is to let programmers define their own types that are as easy to use as the built-in types.

Some rules to decide which type to use:

  • Use an unsigned type when you know that the values cannot be negative.
  • in practice,long often has the same size as int. If your data values are larger than the minimum guaranteed size of an int, then use long long.
  • If you need a tiny integer, explicitly specify either signed char or unsigned char.DO NOT use short.
  • Use double for floating-point computations; float usually does not have enough precision.
  • The precision offered by long double usually is unnecessary and often entails considerable run-time cost.

out-of-range values:

If we assign an out-of-range value to an object of signed type, the result is undefined. The program might appear to work, it might crash, or it might produce garbage values.
assigning –1 to an 8-bit unsigned char gives that object the value 255.(0-255)

posted @ 2022-07-06 20:52  C₅H₁₂O₄季戊四醇  阅读(39)  评论(0)    收藏  举报