C++入门笔记
一直对C++感到很恐惧,大学里有C的基础,今天终于鼓足勇气入门C++,先大致了解一下,以后用到的时候再详细深入。
Android中有一些很火的领域比如:音视频、物联网,都会涉及到JNI、NDK的开发,了解C++还是会很有帮助的。
抽象:
#include <iostream>
using namespace std;
class Shape
{
public:
virtual int getArea() = 0;
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
class Rectangle : public Shape
{
public:
int getArea()
{
return width * height;
}
};
class Triangle : public Shape
{
public:
int getArea()
{
return (width * height) / 2;
}
};
int main(void)
{
Rectangle Rect;
Triangle Tri;
Rect.setWidth(5);
Rect.setHeight(7);
cout << "Total Rectangle area:" << Rect.getArea() << endl;
Tri.setWidth(5);
Tri.setHeight(7);
cout << "Total Triangle area:" << Tri.getArea() << endl;
return 0;
}
常量
#include <iostream>
using namespace std;
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0;
}
#include <iostream>
using namespace std;
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0;
}
继承:
#include <iostream>
using namespace std;
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
class PaintCost
{
public:
int getCost(int area)
{
return area * 70;
}
};
class Rectangle : public Shape, public PaintCost
{
public:
int getArea()
{
return width * height;
}
};
int main(void)
{
Rectangle Rect;
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
cout << "Total area:" << Rect.getArea() << endl;
cout << "Total paint cost:$" << Rect.getCost(area) << endl;
return 0;
}
文件读写:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
char data[100];
// 以写模式打开文件
ofstream outfile;
outfile.open("afile.dat");
cout << "Writing to the file" << endl;
cout << "Enter your name: ";
cin.getline(data, 100);
// 向文件写入用户输入的数据
outfile << data << endl;
cout << "Enter your age: ";
cin >> data;
cin.ignore();
// 再次向文件写入用户输入的数据
outfile << data << endl;
// 关闭打开的文件
outfile.close();
// 以读模式打开文件
ifstream infile;
infile.open("afile.dat");
cout << "Reading from the file" << endl;
infile >> data;
// 在屏幕上写入数据
cout << data << endl;
// 再次从文件读取数据,并显示它
infile >> data;
cout << data << endl;
// 关闭打开的文件
infile.close();
return 0;
}
友元函数
#include <iostream>
using namespace std;
class Box
{
double width;
public:
friend void printWidth(Box box);
void setWidth(double wid);
};
void Box::setWidth(double wid)
{
width = wid;
}
void printWidth(Box box)
{
cout << "Width of box:" << box.width << endl;
}
int main()
{
Box box;
box.setWidth(10.0);
printWidth(box);
return 0;
}
命名空间
#include <iostream>
using namespace std;
namespace first_space
{
void func()
{
cout << "Inside first_space" << endl;
}
} // namespace first_space
namespace second_space
{
void func()
{
cout << "Inside second_space" << endl;
}
} // namespace second_space
// int main()
// {
// first_space::func();
// second_space::func();
// return 0;
// }
using namespace first_space;
int main()
{
func();
return 0;
}
生成实例
#include <iostream>
using namespace std;
int main()
{
double *pvalue = NULL;
pvalue = new double;
*pvalue = 29494.99;
cout << "Value of pvalue:" << *pvalue << endl;
delete pvalue;
return 0;
}
多态
#include <iostream>
using namespace std;
class Shape
{
protected:
int width, height;
public:
Shape(int a = 0, int b = 0)
{
width = a;
height = b;
}
virtual int area()
{
cout << "Parent class area:" << endl;
return 0;
}
};
class Rectangle : public Shape
{
public:
Rectangle(int a = 0, int b = 0) : Shape(a, b) {}
int area()
{
cout << "Rectangle class area:" << endl;
return (width * height);
}
};
class Triangle : public Shape
{
public:
Triangle(int a = 0, int b = 0) : Shape(a, b) {}
int area()
{
cout << "Triangle class area:" << endl;
return (width * height / 2);
}
};
int main()
{
Shape *shape;
Rectangle rec(10, 7);
Triangle tri(10, 5);
shape = &rec;
shape->area();
shape = &tri;
shape->area();
return 0;
}
修饰符
#include <iostream>
using namespace std;
class Line
{
public:
double length;
void setLength(double len);
double getLength(void);
};
double Line::getLength(void)
{
return length;
}
void Line::setLength(double len)
{
length = len;
}
int main()
{
Line line;
line.setLength(6.0);
cout << "Length of line:" << line.getLength() << endl;
line.length = 10.0;
cout << "Length of line:" << line.length << endl;
return 0;
}
多线程
#include <iostream>
#include <pthread.h>
using namespace std;
#define NUM_THREADS 5
void *say_hello(void *args)
{
cout << "Hello Runoob!" << endl;
return 0;
}
int main()
{
pthread_t tids[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; ++i)
{
int ret = pthread_create(&tids[i], NULL, say_hello, NULL);
if (ret != 0)
{
cout << "pthread_create error:error_code=" << ret << endl;
}
}
pthread_exit(NULL);
}
指针
#include <iostream>
using namespace std;
int main()
{
int var1;
char var2[10];
cout << "var1变量的地址:";
cout << &var1 << endl;
cout << "var2变量的地址:";
cout << &var2 << endl;
return 0;
}
C++和Java在语法方面其实有很多想象的地方,毕竟C++也是面向对象的语言