C++怎么开始写啊?

adi 2001-10-06 01:23:30
如题。
...全文
274 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
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
  • 打赏
  • 举报
回复
文档说明
加载更多回复(11)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧