免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 590 | 回复: 0
打印 上一主题 下一主题

C++ tutorial for C users [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-21 14:37 |只看该作者 |倒序浏览
[color="#0f0000"]1.
There is a new way to #include
libraries (the
old method still works yet the compiler roars).
The .h extension is no more written and the names of standard
C libraries are written beginning with a c. In order for the
program to use these libraries correctly using
namespace std;

has to be added:

using namespace std;
#include         // This is a key C++ library
#include cmath>           // The standard C library math.h
int main ()
{
   double a;
   a = 1.2;
   a = sin (a);
   cout g++ test01.cpp -o test01
To run the binary executable file test01 that has been produced
by the compilation (if there were no errors), type this:
./test01
[color="#0f0000"]2.
Input from keyboard and output to screen can be
performed through cout >;

using namespace std;
#include
void main()
{
   int a;                    // a is an integer variable
   char s [100];             // s points to a string of max 99 characters
   cout
   cout end of line)
   cout cin >> a;
   cout > s;
   cout cout "Hello " s " you're " a " old."
   cout
[color="#0f0000"]3.
A global variable can be accessed even if another
variable with the same name has been declared inside the function:
using namespace std;
#include
double a = 128;
int main ()
{
   double a = 256;
   cout ::a using namespace std;
#include
int main ()
{
   double a = 3.1415927;
   double &b = a;                            // b is a
   b = 89;
   cout
[color="#0f0000"]5.
Namespaces can be declared.
The variables declared within a namespace can be used thanks to the ::
operator:
using namespace std;
#include
#include
namespace first
{
   int a;
   int b;
}
namespace second
{
   double a;
   double b;
}
int main ()
{
   first::a = 2;
   first::b = 5;
   second::a = 6.453;
   second::b = 4.1e4;
   cout using namespace std;
#include
#include
int main ()
{
   int a, b;
   cout > a;
   cout try   {      if (a > 100) throw 100;      if (a       throw a / 3;   }   catch (int result)   {      cout       b = result + 1;   }
   cout try   {      if (a == 0) throw zero;      if ((a / 2) * 2 == a) throw pair;      for (int i = 3; i       {         if ((a / i) * i == a) throw notprime;      }      throw prime;   }   catch (char *conclusion)   {      cout    }
   cout
[color="#0f0000"]7.
The OPERATORS OVERLOAD can be used to define the
basic symbolic operators for new sorts of parameters:
using namespace std;
#include
struct vector
{
   double x;
   double y;
};
vector operator * (double a, vector b)
{
   vector r;
   r.x = a * b.x;
   r.y = a * b.y;
   return r;
}
int main ()
{
   vector k, m;              // No need to type "struct vector"
   k.x =  2;                 // To be able to write
   k.y = -1;                 // k = vector (2, -1)
                             // see chapter 19.
   m = 3.1415927 * k;        // Magic!
   cout
struct vector
{
   double x;
   double y;
};
ostream& operator {   o    return o;}
int main ()
{
   vector a;
   a.x = 35;
   a.y = 23;
   cout  
template ttype minimum (ttype a, ttype b){   ttype r;   r = a;   if (b    return r;}
int main ()
{
   int i1, i2, i3;
   i1 = 34;
   i2 = 6;
   i3 = minimum (i1, i2);
   cout double d1, d2, d3;
   d1 = 7.9;
   d2 = 32.1;
   d3 = minimum (d1, d2);
   cout minimum (d3, 3.5) using namespace std;
#include
template
type1 minimum (type1 a, type2 b)
{
   type1 r, b_converted;
   r = a;
   b_converted = (type1) b;
   if (b_converted struct
just contains data. In C++ a struct definition can also include
functions. Those functions are own to the struct and are meant to
operate on the data of the struct. Those functions are called METHODS.
Example below defines the method surface()
on the struct vector:
using namespace std;
#include
struct vector
{
   double x;
   double y;
   double surface ()   {      double s;      s = x * y;      if (s       return s;   }
};
int main ()
{
   vector a;
   a.x = 3;
   a.y = 4;
   cout a.surface() using namespace std;
#include
#include
class array
{
public:
   int size;
   double *data;
   array (int s)
   {
      size = s;
      data = new double ;
   }
   ~array ()
   {
      delete [] data;
   }
   double &operator [] (int i)   {      if (i = size)      {         cerr          exit (EXIT_FAILURE);      }      else return data ;   }
};
int main ()
{
   array t (5);
   t[0] = 45;                       // OK
   t[4] = t[0] + 6;                 // OK
   cout t[4] t[10] = 7;                       // error!
   return 0;
}
[color="#0f0000"]10.
Here is an example of a full class declaration:
using namespace std;
#include
#include
class vector
{
public:
   double x;
   double y;
   vector (double = 0, double = 0);
   vector operator + (vector);
   vector operator - (vector);
   vector operator - ();
   vector operator * (double a);
   double module();
   void set_length (double = 1);
};
vector::vector (double a, double b)
{
   x = a;
   y = b;
}
vector vector::operator + (vector a)
{
   return vector (x + a.x, y + a.y);
}
vector vector::operator - (vector a)
{
   return vector (x - a.x, y - a.y);
}
vector vector::operator - ()
{
   return vector (-x, -y);
}
vector vector::operator * (double a)
{
   return vector (x * a, y * a);
}
double vector::module()
{
   return sqrt (x * x + y * y);
}
void vector::set_length (double a)
{
   double length = this->module();
   x = x / length * a;
   y = y / length * a;
}
ostream& operator vector operator + (vector a, vector b)
{
   return vector (a.x + b.x, a.y + b.y);
}
In the example above of a full class definition, the multiplication
of a vector by a double is defined. Suppose we want the multiplication
of a double by a vector be defined too. Then we must write an isolated
function outside the class:
vector operator * (double a, vector b)
{
   return vector (a * b.x, a * b.y);
}
Of course the keywords new and delete work for
class instances too. What's more, new automatically calls the
constructor in order to initialize the objects, and delete
automatically calls the destructor before deallocating the zone of
memory the instance variables take:
[color="#0f0000"]11.
Let's talk about input/output. In C++ that's a
very broad subject.
Here is a program that writes to a file:
using namespace std;
#include
#include
int main ()
{
   fstream f;
   f.open("c:\\test.txt", ios::out);
   f
Here is a program that reads from a file:
using namespace std;
#include
#include
int main ()
{
   fstream f;
   char c;
   cout







               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/15867/showart_306119.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP