Essential C++学习笔记 第一章 C++编程基础

本文介绍了C++编程的基础知识,包括函数、类、对象、头文件、命名空间等内容,并通过实例展示了如何编写简单的C++程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2017 /3 /9

1.1  如何撰写C++程序

要求:能够将一段信息送至用户的终端机上,信息的内容是要求用户键入自己的名字,然后程序读取名字,并存储,以便接下来的后续操作。最后,送出一个信息,以指名道姓的方式对用户打招呼。


知识:

一、

函数能执行一些运算,包含返回值类别(return type)、函数名、参数列(parameter list)、函数体

1.返回值表示运算结果,main()函数返回整数类别  (所以是int main()  )。无误时令main()返回0,有误时返回非零值。

2.无

3.参数列用来表示“函数执行时,调用者可以传给函数的类型列表

4.无


二、

 class的定义分为两部分,分别写于不同的文件,一个是“头文件”,用来声明该class所提供的各种操作行为。另一个是“程序代码文件”,包含这些操作行为的实现内容。

要使用clas,必须先在程序中放入class的头文件,让程序知道class的定义。 

 例: iostream是一个class,其头文件为#include<iostream>. 这个iostream类的对象有cout、cin

         string是一个class,其头文件为#include<string>  这个string的对象为user_name  (user_name对象用于存储数据,string是它的数据类型,user_name是它的识别名称)


三、

命名空间(namespace)是一种将程序库名称封装起来的方法。例:std是标准程序库所在的命名空间的名称,标准程序库所提供的任何事物(如string class以及cout、cin这两个iostream类对象)都被封装在命名空间std里面

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string first_name,last_name;
	cout << "Please enter your first name and your last name";
	cin >> first_name >> last_name;
	cout << "\n"
		<< "Hello,"
		<< first_name
		<< last_name
		<< "...and goodbye!\n";
	system("pause");
	return 0;
}




练习1.3



1.4



模运算(求余运算)

要求:   

假设我们希望打印的数据每行不超过8个字符串尚未满8个字符串时,就在字符串后打印出一个空格符,如果已满8个字符,就在字符串后打印出一个换行字符

const int line_size = 8;
int cnt = 1;
cout << a_string
       << ( cnt % line_size ? ' ' : '\n');


相等运算符检验用户的回答

if ( user_rsp == 'N' || user_rsp == 'n' )
     user_more = false;




总结: 类、对象、头文件、命名空间、 基本表达式



3/10

条件语句:if 、 if-else 、 if-else-if、switch

循环语句: while 


C++允许我们以内建的array(数组)型别或标准程序库提供的vector类来定义容器,

array:

const int seq_size = 18;

int pell_seq[ seq_size ];

 

vector:

#include<vector>

vector<int> pell_seq ( seq_size ); 


for循环:

for ( init-statement(1) : condition(2) : expression(4) )

     statement(3)         //数字代表执行顺序


指针

int *pi;  //pi是个int型别的对象的指针 ,*pi即提领pi,取得位于该指针所指内存地址上的对象

&ival;  //取ival所在的内存地址

int *pi = &ival;  //将pi的初值设为ival所在的内存地址     


练习1.7

练习1.7
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
using namespace std;
int main()
{
	ifstream in_file("C:\\My Documents\\text.txt");
	if (!in_file)
	{ cerr << "oops! unableto open input file\n";return -1; }
	ofstream out_file("C:\\My Documents\\text.sort");
	if (!out_file)
	{ cerr << "oops! unableto open input file\n";return -2; }

	string word;
	vector< string > text;
	while (in_file >> word)
		text.push_back(word);

	int ix;
	cout << "unsorted text:\n";
	for (ix = 0; ix < text.size();++ix)
		cout << text[ix] << ' ';
	cout << endl;
	sort(text.begin(), text.end());
	out_file << "sorted text:\n";
	for (ix = 0;ix < text.size();++ix)
		out_file << text[ix] << ' ';
	out_file << endl;
	system("pause");
	return 0;
}

59页  调用函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值