课堂笔记
构造与析构
#include <iosteam>
#include <cstring>
using namespace std;struct Date
{int y, m, d;void setDate(int, int, int);Date(int yy, int mm, int dd) {y = yy, m = mm, d = dd;}
};class Student
{
private:char* name;Date birthday;
public:Student(const Student&); Student():birthday(2000, 3, 4) {} Student(char*, int, int, int);~Student() {delete []name;}
};Student::Student & operator = (const Student &s1)
{name = new char[strlen(s1.name) + 1];strnpy(name, s1.name, strlen(s1.name));birthday = s1.birthday;return *this;
}Student::Student(const Student &s1) :birthday(s1.birthday)
{name = new char[strlen(s1.name) + 1];strncpy(name, s1.name, strlen(s1.name));
}Student:: Student(char* p, int y, int m, int d): birthday(y, m, d)
{name = new char[strlen(p) + 1]; strncpy(name, p, strlen(p));
};void print(Student s)
{cout << s,name << endl;
}int main()
{const char *p = "zhangsan";Student s1((char*)p, 2000, 3, 4); Student s2(s1); Student s3;s3 = s1; return 0;
}
静态与友元
#include <iostream>
using namespace std;class Student;
class Date
{string printDate(Student*);
};int studentCount()
{static int count = 0; count++;return count;
}class Student
{
private:string name; int num;int age;static int count; static int totalage;
public:Student(string, int, int);void total(); static float averageAge(); friend void print(Student *); friend string Date::printDate(Student*);friend Date;
};Student::Student(string name1, int num1, int age1):name(name1), num(num1);
{age = age1;
}void Student::total()
{totalage += age;count++;
}float Student::averageAge()
{return totalage / count;
}int Student::count = 0;
int Student::totalage = 0;void print(Student *s)
{s.name = string("class") + s.name; cout << s -> name<< s -> num<< s -> age;
}int main()
{Student s[3] = {Student("zhang", 202301, 18),Student("li", 202302, 19),Student("wang", 202303, 18)};for (int i = 0; i < 3; i++){s[i].total();}Student st0(s[0]);Student st1("liu", 202304, 17);st1 = st0;cout << "average age = " << Student::averageAge(); cout << "average age = " << s[0].averageAge(); return 0;
}
string
#include <iosteam>
using namespace std;int main()
{string s1;string s2("hello world\n");cout << s2[4]; s1 = s1 + s2;cout << s1;
}
继承
#include <iostream>
using namespace std;class Student
{
private:
protected: string name; int num;int age;
public:Student(string, int, int);void print1() {cout << name << num << age;}~Student() {cout << "Destruct Student\n";}
};Student::Student(string name1, int num1, int age1):name(name1), num(num1);
{age = age1; cout << "Construct Student\n";
}class IOTStudent : public Student
{
private:int cpp;
public:IOTStudent(string, int, int, int, int);void print() {cout << name << num << age << cpp;} ~IOTStudent() {cout << "Destruct IOTStudent\n";}
};
IOTStudent::IOTStudent(string name1, int num1, int age1, int cpp): Student(name1, num1, age1)
{cpp = cpp1;cout << "Construct IOTStudent\n";
}void f(Student *p)
{p->print();
}int main()
{Student s1("zhang", 20230001, 19), *p1; IOTStudent s2("li", 20230002, 19, 86), *p2; f(&s1); f(&s2); p1 = &s1;p1->print(); p1 = &s2;p1->print();IOTStudent *p2;p2 = &s2; p2->print(); s1 = s2; p1 = &s1; p2 = &s2;p1 = &s2; p2 = &s1; p1 = p2; p2->print(); s2 = s1; cout << "size of Student is:" << sizeof(Student) << endl;cout << "size of IOTStudent is:" << sizeof(IOTStudent) << endl;cout << s2.name; s2.print(); s2.print1(); s2.Student::print(); return 0;
}
动态联编
#include <iostream>
using namespace std;class Student
{
private:
protected: string name; int num;int age;
public:Student(string, int, int);virtual void print() {cout << name << num << age;} ~Student() {cout << "Destruct Student\n";}
};Student::Student(string name1, int num1, int age1):name(name1), num(num1);
{age = age1; cout << "Construct Student\n";
}class IOTStudent : public Student
{
private:int cpp;
public:IOTStudent(string, int, int, int, int);virtual void print() {cout << name << num << age << cpp;} ~IOTStudent() {cout << "Destruct IOTStudent\n";}
};
IOTStudent::IOTStudent(string name1, int num1, int age1, int cpp): Student(name1, num1, age1)
{cpp = cpp1;cout << "Construct IOTStudent\n";
}void f(Student *p)
{p->print();
}int main()
{Student s1("zhang", 20230001, 19), *p1; IOTStudent s2("li", 20230002, 19, 86), *p2; Student *p1;p1 = &s2;f(p1); IOTStudent *p2;p2 = &s2;f(p2); Student *p;p = &s1;p -> print();p = &s2;p -> print();return 0;
}
动态联编的例子
#include <iostream>
using namespace std;class Shape
{
protected:double L, H;
public:void init() {cout << "input L=";cin >> L;cout << "input H=";cin >> H;}virtual double area() = 0;
};class Tr : public Shape
{
public:virtual double area() {return L * H / 2;}
};class Re : public Shape
{
public:virtual double area() {return L * H;}
};class Ci : public Shape
{
public:virtual double area() {return 3.14 * 3.14 * L;}
};int main()
{Shape *p; for (int i = 0; i < 5; i++){char c;cout << "\ninput shape type";cin >> c;if (c == 'T'){p = new Tr;}else if (c == 'R'){p = new Re;}else if (c == 'C'){p = new Ci;}else{break;}p->init(); cout << "\narea of p is:" << p->area();}delete p;return 0;
}
+±-重载
#include <iostream>
using namespace std;class Complex
{
private:double x, y;
public:Complex(double xx = 0, double yy = 0); void print();Complex& operator++();Complex operator++(int);friend Complex& operator--(Complex& c);
};Complex::Complex(double xx = 0, double yy = 0)
{x = xx;y = y;
}void Complex::print()
{cout << x;if (y > 0){cout << "+" << y << 'i';}else if (y < 0){cout << y << 'i';}
}Complex& operator++()
{x++;return *this;
}Complex Complex::operator++(int)
{Complex temp(*this); x++;return temp;
}Complex& operator--(Complex& c)
{c.x -= 1;return c;
}int main()
{Complex c2(1.1, -2.2);++c2; cout << c2 << endl;c2++; cout << c2;--c2;cout << c2;return 0;
}
函数模板
#include <iostream>
using namespace std;class Complex
{
private:double x, y;
public:Complex(double xx = 0, double yy = 0); bool operator> (Complex& c);friend ostream& operator<< (ostream&, const Complex&);
};Complex::Complex(double xx = 0, double yy = 0)
{x = xx;y = y;
}Complex operator>(bool& c)
{return (x * x + y * y > c.x * c.x + c.y * c.y) ? this : c;
}ostream& operator<< (ostream& o, const Complex& c)
{o << c.x;if (c.y > 0){o << '+' << c.y << 'i';}else if (c.y < 0){o << c.y << 'i';}return o;
}template <typename T1>
T1 Max (T1 x, T1 y)
{return (x > y) ? x : y;
}int Max(int x, int y)
{return (x > y) ? x : y;
}int main()
{int i1 = 4, i2 = 6;cout << Max(i1, i2) << endl;float f1 = 1.2, f2 = 2.3;cout << Max(f1, f2) << endl;char c1 = 'a', c2 = 'x';cout << Max(c1, c2) << endl;Complex com1;com1 = 1; Complex com(1), com(1.1, 1.2);cout << Max(com1, com2) << endl; return 0;
}
类的模板
#include <iostream>
using namespace std;template <typename T2>
class List
{
private:int n;T2 *v;
public:List(int x);~List();T2& operator [](int x);int search(T2 value);
};template <typename T2>
List<T2>::List(int x)
{n = x;v = new T2[n];
}template <typename T2>
List<T2>::~List()
{delete []v;
}T2& List<T2>::operator [](int x)
{return v[x];
}template <typename T2>
int List<T2>::search(T2 value)
{for (int i = 0; i < n; i++){if (v[i] == value) {return i;}}return -1;
}int main()
{typedef List<int> ilist;ilist il2(57);List<int> il1(57); List<float> fl1(58);fl1[20] = 5.0;List<Complex> c1(59); return 0;
}template <typename T1>
class Mylnt
{
private:T1 t1, t2, t3;
public:Mylnt();Mylnt(T1 t1_value, T1 t2_value, T1 t3_value);T1 getMax();T1 getMin();void sort();void show();
};template <typename T1>
Mylnt<T1>::Mylnt(T1 t1_value, T1 t2_value, T1 t3_value)
{t1 = t1_value;
}template <typename T1>
T1 Mylnt<T1>::getMax()
{T1 max;if (t1 >= t2){if (t1 > t3){max = t1;}else{max = t3;}}else{if (t2 > t3){max = t2;}else{max = t3;}}
}
运算符重载
#include <iostream>
using namespace std;class Complex
{
private:double x, y;
public:Complex(double xx = 0, double yy = 0); void print();Complex& add(const Complex& c2);Complex& operator+=(const Complex& c2); Complex operator+(const Complex& c2); friend Complex operator-(const Complex& c1, const Complex& c2);
};Complex::Complex(double xx = 0, double yy = 0)
{x = xx;y = y;
}void Complex::print()
{cout << x;if (y > 0){cout << "+" << y << 'i';}else if (y < 0){cout << y << 'i';}
}Complex& Complex::add(const Complex& c2)
{x += c2.x;y += c2.y;return Complex(x, y); return *this;
}Complex& Complex::operator+=(const Complex& c2)
{x += c2.x;y += c2.y;return *this;
}Complex Complex::operator+(const Complex& c2)
{Complex temp;temp.x = c2.x + this->x;temp.y = c2.y + y;return temp;
}Complex operator-(const Complex& c1, const Complex& c2)
{Complex t;t.x = c1.x - c2.x;t.x = x + c2.x; t.y = c1.y - c2.y;return t;
}int main()
{Complex c1;Complex c2(1.1, -2.2);Complex c3(1.3, 4);cout << "\nc1="; c1.print;cout << "\nc2="; c2.print;cout << "\nc3="; c3.print;c1.add(c2); cout << "\nafter add(), c1= "; c1.print;c1 += c2; cout << "\nafter +=c2, c1= "; c1.print;c3 = c1 + c2; cout << "\nafter c3=c1+c2, c3= "; c3.print;c3 = c1 - c2; (c1 + c2 + c3); return 0;
}
int getmax(int x, int y)
{return (x > y) ? x : y;
}
c3 = c1 & c2;
c1 &= c2;M1 = M1 * M2;
输入输出重载
#include <iostream>
using namespace std;class Complex
{
private:double x, y;public:Complex(double xx = 0, double yy = 0); friend ostream &operator<<(ostream &, const Complex &); friend istream &operator>>(istream &, Complex &);
};Complex::Complex(double xx = 0, double yy = 0)
{x = xx;y = yy;
}ostream &operator<<(ostream &o, const Complex &c)
{o << c.x;if (c.y > 0){o << "+" << c.y << 'i';}if (c.y < 0){o << c.y << 'i';}return o;
}istream &operator>>(istream &, const Complex &)
{}int main()
{Complex c1;Complex c2(1.1, -2.2);Complex c3(1.3, 4);cout << c1 << c2;return 0;
}
期末
#include <iostream>
#include <cstring>
using namespace std;class A
{
private:char *name;int i;
public:A(const char*);~A();A(const A&); A& operator=(const A&); void print() {cout << "this is class A\n";}
};A::A(const char* n)
{name = nullptr;size_t size = strlen(n);name = new char[size + 1];cout << "allocate memory for name\n";strncpy(name, n, size);cout << "construct A : " << name << endl;
}A::~A()
{cout << "destruct A : " << name << endl;delete[] name;
}A::A(const A& a)
{if (name != nullptr){delete[] name;}name = a.name; size_t size = strlen(a.name); strncpy(name, a.name, size);
}class B : public A
{
public:void print() {cout << "this is class B\n";}
};class C : public A
{
public:void print() {cout << "this is class C\n";}
};class D : public A
{
public:
};int main()
{A *p, b[5];p = new A[5];cout << b[2].i;cout << (*(p + 2)).i;delete[] p;A a("OOP class"), *ap;A b("class 2");A b(a); A b = a; cout << "a.name=" << (void*)a.name << "b,name=" << (void*)b.name << endl; a = b; cout << "a.name=" << (void*)a.name << "b,name=" << (void*)b.name << endl;a.print(); ap = &a;ap -> print(); ap -> print(); return 0;
}
比较生日
#include <iostream>
using namespace std;struct CDate
{int y, m, d;
};class CPerson
{
private:CDate birthday;public:bool cmp(CDate c) {long l1, l2;l1 = birthday.y * 10000 + birthday.m * 100 + birthday.d;l2 = c.y * 10000 + c.m * 100 + c.d;return l1 > l2;}
};int main()
{CPerson p1, p2;return 0;
}
多重继承
#include <iostream>
using namespace std;class Person
{
protected:string name;Person(string n) {name = n;}
}class Student : virtual public Person
{
protected:int id;
public:Student() : Person("") {} Student(string n, int i) : Person(n), id(i) {cout << "CS\n";} ~Student() {cout << "DS\n";}void print() {cout << name << id;}
};class Teacher : virtual public Person
{
protected:double salary;
public:Teacher(string, double) : Person(n){salary = s; cout << "CT\n";} ~Teacher() {cout << "DT\n";}void print() {cout << name << salary;}
};
class SJ : public Student, public Teacher
{
public:SJ(string, int, double);~SJ() {cout << "Dsj\n";}void print();
};SJ::SJ(string n, int i, double s) : Person(n), Student(n, i), Teacher(n, s)
{cout << "Csj\n";
}void SJ::print()
{cout << name << id << name2 << salary;print1(); print2();Student::print();Teacher::print();cout << name << id << salary; cout << Teacher::name << id << salary; cout << name << id << salary;
}int main()
{SJ sj("li", 20230001, 4000.0);return 0;
}