Programming Principles and Practice Using C++ Notes2

第三章对象、类型和值

对象:用来保存一个指定类型值的一些内存单元。

类型:定义一组可能的值与一组运算(对于一个对象)。

值:根据一个类型来解释的内存中的一组比特。

#include <iostream>
using namespace std;
int main()
{
        cout << "Please enter your first name and age\n"<< endl;

        string first_name;
        int age;
            
        cin >> first_name; 
        cin >> age;
        cout << "Hello,"<< first_name << "(age" << age <<")\n"<< endl;

        return 0;
}

这里我曾犯了个错误。我是这么写的(想当然了),结果g++了一堆错误提示。

cin >> first_name >> endl;
cin >> age >> endl;

 “字符串+”意味着“连接”,也就是说:

当S1 和 S2是字符串时, S1+S2也是字符串,包含来自S1的字符后接来自S2的多个字符。

例如:S1的值为“Hello",S2的值为”World”,那么S1+S2的值为“Hello World”

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6         cout << "Please enter your first ande second names:\n"<< endl;
 7     
 8         string s1;
 9         string s2;
10 
11         cin >> s1 >> s2;     //read two strings
12 
13         string name = s1 +''+ s2; //concatenate strings
14 
15         cout << "Look,"<< name <<'\n';
16 
17         return 0;
18 
19 }

g++报错提示:13: error: empty character constant

string name = s1 +' '+ s2; //单引号中间应有空格

 

转载于:https://www.cnblogs.com/Charons/p/11171520.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Bjarne Stroustrup, "Programming: Principles and Practice Using C++"Addison-Wesley Professional | 2008 | ISBN: 0321543726 | 1272 pages | PDF | 129 MBAn Introduction to Programming by the Inventor of C++Preparation for Programming in the Real WorldThe book assumes that you aim eventually to write non-trivial programs, whether for work in software development or in some other technical field.Focus on Fundamental Concepts and TechniquesThe book explains fundamental concepts and techniques in greater depth than traditional introductions. This approach will give you a solid foundation for writing useful, correct, maintainable, and efficient code.Programming with Today’s C++The book is an introduction to programming in general, including object-oriented programming and generic programming. It is also a solid introduction to the C++ programming language, one of the most widely used languages for real-world software. The book presents modern C++ programming techniques from the start, introducing the C++ standard library to simplify programming tasks.For Beginners–And Anyone Who Wants to Learn Something NewThe book is primarily designed for people who have never programmed before, and it has been tested with more than 1,000 first-year university students. However, practitioners and advanced students will gain new insight and guidance by seeing how a recognized master approaches the elements of his art.Provides a Broad ViewThe first half of the book covers a wide range of essential concepts, design and programming techniques, language features, and libraries. Those will enable you to write programs involving input, output, computation, and simple graphics. The second half explores more specialized topics, such as text processing and testing, and provides abundant reference material. Source code and support supplements are available from the author’s website.Part 1depositfiles.comuploading.commirrorPart 2depositfiles.comuploading.commirrorNot all books on AvaxHome appear on the
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值