C++怎么开始写啊?

adi 2001-10-06 01:23:30
如题。
...全文
213 点赞 收藏 31
写回复
31 条回复
a_programer 2001年10月12日
没有事做的时候,多看看书。
回复 点赞
qsyang 2001年10月12日
up
回复 点赞
genius007 2001年10月12日
哈哈,按书写,哈哈
回复 点赞
Herakles 2001年10月12日
推荐你一本书《C++面向对象教程》。
回复 点赞
TopCat 2001年10月11日
标准C++使用int main(),贴一段Bjarne Stroustrup的言论(不翻了,没时间):

Can I write "void main()"?
------------------------------------
The definition
void main() { /* ... */ }

is not and never has been C++, nor has it even been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1. A conforming implementation accepts
int main() { /* ... */ }

and
int main(int argc, char* argv[]) { /* ... */ }

A conforming implementation may provide more versions of main(), but they must all have return type int. The int returned by main() is a way for a program to return a value to "the system" that invokes it. On systems that doesn't provide such a facility the return value is ignored, but that doesn't make "void main()" legal C++ or legal C. Even if your compiler accepts "void main()" avoid it, or risk being considered ignorant by C and C++ programmers.
In C++ main() need not contain an explicit return statement. In that case, the value returned is 0, meaning successful execution. For example:

#include<iostream>

int main()
{
std::cout << "This program returns the integer value 0\n";
}

Note also that neither ISO C++ nor C99 allows you to leave the type out of a declation. That is, in contrast to C89 and ARM C++ ,"int" is not assumed where a type is missing in a declaration. Consequently:
#include<iostream>

main() { /* ... */ }

is an error because the return type of main() is missing.

再贴一段这位“C++之父”的“非常简单的小程序”的写法:

How do I write this very simple program?
---------------------------------------------
Often, especially at the start of semesters, I get a lot of questions about how to write very simple programs. Typically, the problem to be solved is to read in a few numbers, do something with them, and write out an answer. Here is a sample program that does that:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
vector<double> v;

double d;
while(cin>>d) v.push_back(d); // read elements
if (!cin.eof()) { // check if input failed
cerr << "format error\n";
return 1; // error return
}

cout << "read " << v.size() << " elements\n";

reverse(v.begin(),v.end());
cout << "elements in reverse order:\n";
for (int i = 0; i<v.size(); ++i) cout << v[i] << '\n';

return 0; // success return
}

Here are a few observations about this program:
This is a Standard ISO C++ program using the standard library. Standard library facilities are declared in namespace std in headers without a .h suffix.
If you want to compile this on a Windows machine, you need to compile it as a "console application".
Yes, main() returns an int.
Reading into a standard vector guarantees that you don't overflow some arbitrary buffer. Reading into an array without making a "silly error" is beyond the ability of complete novices - by the time you get that right, you are no longer a complete novice. If you doubt this claim, I suggest you read my paper "Learning Standard C++ as a New Language", which you can download from my publications list.
The !cin.eof() is a test of the stream's format. Specifically, it tests whether the loop ended by finding end-of-file (if not, you didn't get input of the expected type/format). For more information, look up "stream state" in your C++ textbook.
A vector knows its size, so I don't have to count elements.
for reading in strings, see How do I read a string from input?.
For more examples of how to use the standard library to do simple things simply, see the "Tour of the Standard Library" Chapter of TC++PL3 (available for download).

希望有一点帮助。谢谢!
回复 点赞
fiveyes 2001年10月11日
找本全国计算机等级考试的二级教程(C语言版),然后在互联网上(如果你用来学习的那台电脑能上网的话)以Turbo为关键字搜一个Turbo C2.0版,下载。剩下的任务就是对着书逐字逐句逐章逐节逐题逐答案的啃下来。
谈到C++(终于说到C++了),当你找到一本C++的入门教程的时候,会发现它与C是那么的相似,据说当初给C++命名的时候,有人就建议叫“含有类的C”。
不过这些都不是关键,学好C++,功夫在C++外,或者说,程序员的主要学习内容不是编码。据说在软件工程中,编码的比重顶多占到30%,软件结构的设计更重要,软件的总体设计是最重要的,在总体设计之前的可行性研究及需求分析是最最重要的,软件完成之后的测试维护是最最最重要的。
回复 点赞
swat 2001年10月11日
用笔,或用键盘,真笨,这还用人教!
回复 点赞
QXLEE 2001年10月10日



好像同意用void的更多,我同意
回复 点赞
cowputer 2001年10月10日
初学c时都是先用main()
而初学c++时都是先用void main()
因为主函数也有返回值
回复 点赞
albertlee 2001年10月10日
天那。
回复 点赞
瑶瑶爸爸 2001年10月10日
cout<<"Hello,World!"<<endl;
回复 点赞
davidlxm 2001年10月10日
class class_name public : ....
{
};
不然用C算了。
回复 点赞
wondful 2001年10月10日
main()也一样,缺省嘛
回复 点赞
liuer 2001年10月09日
强烈郑重庄严隆重推荐使用:void main()
回复 点赞
lzumcj 2001年10月09日
c++中main()不是最佳方案,会有waring message。因为c++是强制型语言
所以建议写成:return_type main(){ },如:
void main(){ }
int main(){ }
等。。。
回复 点赞
djwinter 2001年10月09日
void main()
回复 点赞
间谍 2001年10月09日
到底void main()是标准的写法还是main()是标准的写法呀?
回复 点赞
cowputer 2001年10月09日
大家好,
我想应该是先读读一些代码就会知道了
如#include #<iostream.h>
void main()
{
int x,y,z;*/定义三个变量x,y,z/*
x=3;*/给x赋值3/*
y=4;*/给y赋值4/*
z=x+y;*/把x和y的和赋给z/*
cout<<z<<endl;*/输出z的值/*

}
程序结束,
这大概是最简单的,
不知可懂了没?
回复 点赞
eternalee 2001年10月07日
#include <iostream>

void main(void)
{
cout<<"Hello,World!"<<endl;
}
回复 点赞
krerix 2001年10月07日
文档说明
回复 点赞
发动态
发帖子
C语言
创建于2007-09-28

3.2w+

社区成员

24.0w+

社区内容

C语言相关问题讨论
社区公告
暂无公告