C++ Primer Chapter 1. Getting Started

Chapter 1. Getting Started

Generally introduces most of the basic elements of C++: types, variables, expressions, statements, and functions. You should be able to write, compile, and execute simple programs.

In this chapter, we’ll write a program to solve a simple problem for a bookstore.

0-201-70353-X 4 24.99
ISBN–number of copies sold–the price

1.1. Writing a Simple C++ Program

Every C++ program at least contains one function: main. The operating system runs a C++ program by calling it.

A function definition has:

  • a return type. The main function is required to have a return type of int type-A type defines both the contents of a data element and the operations that are possible on those data. When the type of a variable named v is T, we often say that “v has type T” or, interchangeably, that “v is a T.”
  • a function name,
  • a (possibly empty) parameter list enclosed in parentheses
  • a function body:

at the end of a function body:

return 0; //Semicolons mark the end of most statements in C++. They are easy to overlook but, when forgotten, can lead to mysterious compiler error messages.

terminates a function & send a value (the type of which is the same as return type) back to the function’s caller.

On most systems, the value returned from main is a status indicator. A return value of 0 indicates success. A nonzero return has a meaning that is defined by the system. Ordinarily a nonzero return indicates what kind of error occurred.

1.1.1 Compiling and Executing Our Program

Most compilers, including those that come with an IDE, provide a command-line interface. Unless you already know the IDE, you may find it easier to start with the command-line interface. Doing so will let you concentrate on learning C++ first. Moreover, once you understand the language, the IDE is likely to be easier to learn.

most compilers expect program source code to be stored in one or more files. Program files are normally referred to as a source files. On most systems, the name of a source file ends with a suffix. Different compilers use different suffix conventions; the most common include .cc, .cxx, .cpp, .cp, and .C.

If we are using a command-line interface, we will typically compile a program in a console window- for example, a shell window on a UNIX system. The most common compilers are the GNU compiler g++.

$ g++ -o -Wall prog1 prog1.cc //The compiler generates an executable file, -o prog1 is an argument to the compiler and names the executable file:prog1(no suffix). Without this it  put the executables in files named a.out. Our program file/source file is prog1.cc Compilers usually include options to generate warnings about problematicconstructs. It is usually a good idea to use these options. Our preference isto use -Wall with the GNU compiler,

To run an executable:

./a.out //a “.” followed by a forward slash to indicate that our executable is in the current directory.

The value returned from main is accessed :

$ echo $?

1.2. A First Look at Input/Output

C++ doesn’t define any IO statements. They are provided by the IO library in the standard library. We mostly use iostream library. Two types named istream and ostream(input and output stream. A stream is a sequence of characters read from or written to an IO device.) are fundaments.

Standard Input and Output Objects

what are they?

​ what is standard library?

What do they do?

how are they associated?

how to write a program that use IO objects cin and cout and show the sum?

​ header syntax ,usage?

​ what is expression? The << operator syntax?

​ what is string literal, endl?

what is namespace? std? syntax?

Kinds of Comments in C++? syntax?

What is flow of control (statements)?

​ while syntax?

​ for feature? syntax?

How to read an Unknown Number of Inputs?

​ when does istream become invalid?

if syntax?

How to define our own data structure in C++ ?

Class

What is a class? class type? What’s the difference? why use class?

To use a class we need to know which three things?

what is headers?

Do we need to concern how data are stored or computed in a class?

  • No. What we only need to know is what operations (aka behavior,actions) objects of that type can perform.

what does operations of a class mean?

How to define a variable of a class (type) aka an object of a class (type)?

  • ex: That is, the Sales_item class defines what happens when a Sales_item object is created and what happens when the assignment, addition, or the input and output operators are applied to Sales_items.

How do we call an object of a xx class (type)?

  • an object of type xx, a xx object, a xx.

What’s the differences between angle brackets and double quotes of the headers?

  • angle brackets indicates the system directory and double quotes indicates the current directory.

    In practice, the difference is in the location where the preprocessor searches for the included file.

    For #include <filename> the preprocessor searches in an implementation dependent manner, normally in search directories pre-designated by the compiler/IDE. This method is normally used to include standard library header files.

    For #include "filename" the preprocessor searches first in the same directory as the file containing the directive, and then follows the search path used for the #include <filename> form. This method is normally used to include programmer-defined header files.

What Is a Member Function aka method? syntax?methods vs operations (aka behavior,actions)? What is MEMBER is member the member function?

Questions

​ P42

Thus, our while executes until we encounter end-of-file (or an input error).

when inputs meet the end , what is the expression std::cin >> value doing? The expression std::cin >> i1 >> i2 in former program to get the input values doen’t stuck, right?

#include <iostream>
int main()
{
int sum = 0, value = 0;
// read until end-of-file, calculating a running total of all values read
while (std::cin >> value)
sum += value; // equivalent to sum = sum + value
std::cout << "Sum is: " << sum << std::endl;
return 0;
}

What is class type ? :

class: Facility for defining our own data structures together with associated operations.

Besides: A class defines a type along with a collection of operations that are related to that type.

In fact, a primary focus of the design of C++ is to make it possible to define class types that behave as naturally as the built in types.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值