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++也是面向对象的语言
分类:
C++
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2018-03-16 《Gradle权威指南》--Gradle构建脚本基础
2018-03-16 《Gradle权威指南》--Groovy基础
2018-03-16 《gradle权威指南》--Gradle入门
2017-03-16 RecyclerView 和 ListView 使用对比分析
2017-03-16 apk瘦身
2016-03-16 Android超链接