2023-1-7 markclemens
文章目录
hello.cpp
#include<iostream>
using namespace std;
int main() {
char c;
bool ischar;
int isLowercaseVowel,isUppercaseVowel;
cout<<"输入一个字母:";
cin>>c;
ischar=((c>='a'&&c<='z')||(c>='A'&&c<='Z'));
if(ischar) {
// 小写字母元音
isLowercaseVowel=(c=='a'||c=='e'||c=='i'||c=='o'||c=='u');
// 大写字母元音
isUppercaseVowel=(c=='A'||c=='E'||c=='I'||c=='O'||c=='U');
// if 语句判断
if(isLowercaseVowel||isUppercaseVowel)
cout<<c<<" 是元音";
else
cout<<c<<" 是辅音";
} else {
cout<<"输入的不是字母。";
}
return 0;
}
class_demo.cpp
#include <iostream>
#include <cstring>
using namespace std;
class Box
{
public:
static int box_count;
int length;
int breadth;
int height;
// 构造函数 constructor
Box()
{
cout << "constructor(default)..." << endl;
box_count++;
}
// 析构函数 destructor
~Box()
{
cout << "destructor(default)..." << getName() << endl;
// if (name != NULL) free(name); // also ok
delete name;
}
Box(int, int, int, char[]); //constructor
Box(const Box &); // copy-constructor
// 成员函数声明
static void printCount(void) { cout << "static box count:" << box_count << endl; }
int get(void) { return length * breadth * height; }
char *getName(void) { return this->name; }
void set(int, int, int, char[]);
void display(void);
private:
char *name; //默认情况下,不写修饰符,类的所有成员都是私有的
};
// 静态成员初始化
int Box::box_count = 0;
int main()
{
char p1[] = "box1";
Box box1;
box1.set(3, 4, 5, p1); // constructor(default)
box1.display();
char p2[] = "box2";
Box box2(1, 4, 5, p2); // constructor(with params)
box2.display();
Box box3 = Box(box2), *pBox; // copy constructor()
pBox = &box3;
pBox->display();
Box::printCount();
return 0;
}
// 有参构造函数 + 委托构造函数
Box::Box(int l, int w, int h, char p[]) : Box()
{
cout << "constructor(with params)..." << endl;
this->length = l;
this->breadth = w;
this->height = h;
this->name = p;
}
// 拷贝构造函数
Box::Box(const Box &box) : length(box.length), breadth(box.breadth), height(box.height), name(box.name)
{
cout << "copy constructor()..." << endl;
box_count++;
// name = new char[strlen(box.name) + 1];
strcpy(name, box.name);
strcat(name, "*");
}
// 成员函数定义 ===================================
void Box::set(int length, int breadth, int height, char p[])
{
this->length = length;
this->breadth = breadth;
this->height = height;
this->name = p;
}
void Box::display()
{
clog << "volume: " << get() << ", box_count: " << box_count << endl
<< "length: " << length
<< ",breadth: " << breadth
<< ",height: " << height
<< ",name: " << getName() << endl
<< endl;
}
cout_setw.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setiosflags(ios::right | ios::showpoint); // 设右对齐,以一般实数方式显示
cout.precision(5); // 设置除小数点外有五位有效数字
cout << 123.456789 << endl;
cout.width(10); // 设置显示域宽10
cout.fill('*'); // 在显示区域空白处用*填充
cout << setw(8) << 123.456789 << endl; // setw 只对紧接着的一次输出生效!
char name[50];
cout << "Input a name:";
cin >> name;
clog << "INFO: get a name:" << name << endl;
cerr << "ERROR: this program crash!" << endl;
return 0;
}
friend_demo.cpp
#include <iostream>
using namespace std;
class Box
{
double width;
public:
// 将外部函数注册为友元函数
friend void printWidth(Box box);
// 将外部类注册为友元类
friend class Friend;
void setWidth(double wid)
{
width = wid;
}
};
class Friend
{
public:
char name[12] = "FriendClass";
void Print(int width, Box &box)
{
box.width = width;
cout << "Outer class (friend class) can access Box class: width:" << box.width << endl;
}
};
void printWidth(Box box)
{
cout << "Outer function (friend func) can access Box class: width:" << box.width << endl;
}
int main()
{
Box box;
box.setWidth(123);
printWidth(box); // friend func
Friend f;
f.Print(456, box); // friend class
getchar(); //stop
return 0;
}
inline_demo.cpp
#include <iostream>
using namespace std;
inline int Max(int x, int y)
{
return (x > y)? x : y;
}
// 程序的主函数
int main( )
{
// 内联:编译器在函数调用处直接将函数替换为函数体,类定义里的函数默认是内联函数。 建议内联函数不要超过10行
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;
return 0;
}
lambda_demo.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional> // define lambda
using namespace std;
int main()
{
// Lambda 表达式把函数看作对象
// 在Lambda表达式内可以访问当前作用域的变量,这是Lambda表达式的闭包(Closure)行为。
vector<int> v = {1, 2, 3, 4, 5};
int even_count = 0;
// 对象化,这样是为了其他地方也能用
function<void(int &)> lamb = [&even_count](int &x) mutable -> void
{
if (!(x & 1))
even_count++;
x += 2;
};
for_each(v.begin(), v.end(), lamb);
cout << "Lambda >> even_count: " << even_count << endl;
// lambda: [capture](parameters) mutable ->return-type{statement}
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
return 0;
}
namespace.cpp
#include <iostream>
using namespace std;
// 第一个命名空间
namespace first_space{
void func(){
cout << "Inside first_space" << endl;
}
}
// 第二个命名空间
namespace second_space{
void func(){
cout << "Inside second_space" << endl;
}
}
using namespace first_space;
int main ()
{
// 调用第一个命名空间中的函数
func();
return 0;
}
polymorphism.cpp
#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 area1() = 0;
// 虚函数
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;
// 调用矩形的求面积函数 area
shape->area();
// 存储三角形的地址
shape = &tri;
// 调用三角形的求面积函数 area
shape->area();
return 0;
}
signal_demo.cpp
#include <iostream>
#include <csignal>
#include <unistd.h>
#include <cassert>
using namespace std;
void signalHandler(int signum)
{
cout << "Interrupt signal (" << signum << ") received.\n";
// 清理并关闭
// 终止程序
exit(signum);
}
int main()
{
int i = 0;
// 断言
assert(i == 0);
// assert(i != 0);
// 注册信号 SIGINT 和信号处理程序
signal(SIGINT, signalHandler);
while (++i)
{
cout << "Going to sleep...." << endl;
if (i == 3)
{
raise(SIGINT);
}
sleep(1);
}
return 0;
}
template_demp.cpp
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include <functional>
using namespace std;
template <typename T>
inline T const &Max(T const &ele1, T const &ele2)
{
return (ele1 > ele2) ? ele1 : ele2;
}
template <typename T>
function<void(T &, T &)> lamb = [](T &ele1, T &ele2) -> void
{
const type_info &typeT = typeid(ele1);
cout << "template func -- Max<" << typeT.name() << ">: " << Max(ele1, ele2) << endl;
};
template <class T>
class Stack
{
public:
void push(T const &); // 入栈
void pop(); // 出栈
T top() const; // 返回栈顶元素
bool empty() const
{ // 如果为空则返回真。
return elems.empty();
}
private:
vector<T> elems; // 元素
};
int main()
{
string s1 = "abc", s2 = "ac";
lamb<string>(s1, s2);
int i1 = 15, i2 = 48;
lamb<int>(i1, i2);
double d1 = 123, d2 = 456;
lamb<double>(d1, d2);
Stack<string> stringStack; // string 类型的栈
stringStack.push("hello");
stringStack.push("workd");
cout << "template class -- " << stringStack.top() << endl;
stringStack.pop();
cout << "template class -- " << stringStack.top() << endl;
return 0;
}
template <class T>
void Stack<T>::push(T const &elem)
{
// 追加传入元素的副本
elems.push_back(elem);
}
template <class T>
void Stack<T>::pop()
{
if (elems.empty())
{
throw out_of_range("Stack<>::pop(): empty stack");
}
// 删除最后一个元素
elems.pop_back();
}
template <class T>
T Stack<T>::top() const
{
if (elems.empty())
{
throw out_of_range("Stack<>::top(): empty stack");
}
// 返回最后一个元素的副本
return elems.back();
}
vector_iterator.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void traverse(const void *p)
{
vector<int> v = *(vector<int> *)p;
// 下标访问
for (int i = 0; i < v.size(); i++)
{
cout << "v[" << i << "]" << v[i] << ", ";
}
cout << endl;
// 迭代器访问
vector<int>::iterator it = v.begin();
while (it != v.end())
{
cout << "v[i]" << *it++ << ", ";
}
cout << endl;
}
int main()
{
// Lambda 表达式把函数看作对象
// 在Lambda表达式内可以访问当前作用域的变量,这是Lambda表达式的闭包(Closure)行为。
vector<int> v = {1, 2, 3, 4, 5};
int even_count = 0;
// 偶数计数
for_each(v.begin(), v.end(), [&even_count](int &x)
{
if (!(x & 1))
even_count++;
else
x = 666;
});
cout << "Lambda >> even_count:" << even_count << ". vector size:" << v.size() << endl;
//[capture](parameters) mutable ->return-type{statement}
v.pop_back();
v.push_back(777);
traverse((void *)&v);
return 0;
}
pointer.cpp
#include <iostream>
using namespace std;
int main ()
{
int var1;
char var2[10];
cout << "var1 变量的地址: ";
cout << &var1 << endl;
cout << "var2 变量的地址: ";
cout << &var2 << endl;
return 0;
}