期末复习ppt
作者:互联网
c++复习
第一讲 指针
定义 : 数据类型 *指针名 eg. int *p;
& 取地址
* 获得指针指向的变量内容
//使用不同的方法访问数组元素
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int a[5] = {1, 2, 3, 4, 5};
int *p;
p = a; // p = &a[0];
for (int i = 0; i < 5; i++) {
printf("%d ", a[i]);
//printf("%d",p[i]);
//printf("%d",*(a+i));
//printf("%d",*(p+i));
}
printf("\n");
}
第二讲
指向二维数组的指针
#include <iostream>
using namespace std;
int a[3][4] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
int main() {
int i, j, (*p)[4];
p = a;
for ( i = 0; i < 3; i++) {
for ( j = 0; j < 4; j++) {
printf("%4d", *(*p + j));
}
p++;
printf("\n");
}
return 0;
}
字符数组的输入输出
#include <iostream>
#include <cstring>
using namespace std;
char str[13] = "sfds";
int main() {
scanf("%s", str);//加不加&都可
//str[13] = "sfsdf";不可
printf("%s", str);
}
字符串相关的函数 (很重要但懒得写)
……
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "This is a string.";
char *string;
string = str;
printf("%s\n", str);
printf("%s\n", string);
string += 8;
while (*string) {
putchar(string[0]);
string++;
}
return 0;
}
/*
This is a string.
This is a string.
a string.
*/
第三讲
二级指针的引用
#include <iostream>
#include <cstring>
using namespace std;
int main() {
int a, *pa, **ppa; //**ppa二级指针
a = 100;
pa = &a;
ppa = &pa;
printf("%d,%u,%u\n", a, pa, ppa);
printf("%d,%d,%d\n", a, *pa, **ppa);
printf("%u,%u,%u\n", &a, pa, *ppa);
return 0;
}
/*
100,7995084,7995080
100,100,100
7995084,7995084,7995084
*/
第四讲.结构化编程基础
引用 注意引用不能为空(NULL)
int main() {
int b = 1;
int &a = b;
cout << a << " " << b << endl;
return 0;
}
函数按值传递省略
注意swap3 按指针传递
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
void swap2(int &a, int &b) {//按引用传递
int t;
t = a;
a = b;
b = t;
}
void swap3(int *a, int *b) {
int t;
t = *a;
*a = *b;
*b = t;
cout << t << endl;
}
int main() {
int s1 = 10, s2 = 20;
swap2(s1, s2);
swap3(&s1, &s2);
cout << s1 << " " << s2 << endl;
return 0;
}
引用 = 起一个别的名字
使用引用返回的主要目的是为了将函数用在赋值运算符的左边,否则不可以
int a[] = {0, 1, 2, 3};
int &index(int i) {
return a[i];
}
int main() {
index(3) = 10;
cout << index(3) << endl;
return 0;
}
内联函数:执行速度快,损耗内存,递归函数不能内联
重载函数
inline void print(int a){
cout<<a<<endl;
}//必须写在主函数前面
int sum(int x,int y);
int sum(int x,int y,int z);
void print(double a);
//后面省略
结构:可以省略struct
结构变量 Point p1,p2;
第四讲作业:
#include <iostream>
#include <cstdio>
#include <cmath>
#define pi acos(-1)
using namespace std;
double solve(double x, double y) {
return 2 * (x + y);
}
double solve(double r) {
return 2 * pi * r;
}
int main() {
double x, y, r;
printf("请输入矩形长和宽\n");
scanf("%lf%lf", &x, &y);
printf("矩形的周长为:%lf\n", solve(x, y));
printf("请输入圆的半径\n");
scanf("%lf", &r);
printf("圆的周长为:%lf\n", solve(r));
return 0;
}
第五讲.类与对象
创建类还是比较容易,这里就不写了
对象的创建和销毁:静态分配和动态分配
对对象成员的访问
eg.Point a(1,2); Oneday day(10,2,2022);
下面介绍了指针访问
#include <iostream>
using namespace std;
class Point {
public:
void setxy(int x, int y);
void displayxy();
private:
int X, Y;
};
void Point::setxy(int x, int y) {
X = x;
Y = y;
}
void Point::displayxy() {
cout << "(" << X << "," << Y << ")" << endl;
}
int main() {
Point p1, *p2;
p1.setxy(3, 4);
cout << "第一个点的位置是:";
p1.displayxy();
p2 = &p1;
p2->setxy(5, 6); //注意这个修改要加->
cout << "第二个点的位置是:";
p2->displayxy();
(*p2).setxy(7, 8);
cout << "第二个点的位置是:";
(*p2).displayxy();
return 0;
}
练习写一个Date类
#include <iostream>
using namespace std;
class Date {
public:
Date(int day = 1, int mon = 1, int year = 1000) {
D = day, M = mon, Y = year;
}//用构造函数替代了set函数
bool Isprimeyear() {
//学习了新的写法
return (Y % 4 == 0 && Y % 100 != 0) || (Y % 400 == 0);
}
void print() {
cout << D << "/" << M << "/" << Y << endl;
}
private:
int D, M, Y;
};
int main() {
Date yt(17, 6, 2022);
if (yt.Isprimeyear()) {
cout << "Yes\n";
} else
cout << "No\n";
yt.print();
return 0;
}
第六讲
构造函数举例:
class Clock {
public:
Clock() {
hour = 6;
minute = 30;
second = 30;
}
void showTime() {
cout << hour << ":" << minute << ":" << second << endl;
}
private:
int hour, minute, second;
};
int main() {//因为不太会指针的用法
Clock *pc1 = new Clock;
Clock pc2;
Clock *pc3 = &pc2;
pc1->showTime();
pc2.showTime();
(*pc3).showTime();//注意这里每一种写法都比较固定
return 0;
}
几个构造函数放在一起的用法,注意二义性问题
#include <iostream>
using namespace std;
class Point {
public:
Point();
Point(int);//注意这里可以只放一个int
Point(int, int);
void displayxy() {
cout << "(" << X << "," << Y << ")" << endl;
}
private:
int X, Y;
};
Point::Point() {
X = 1, Y = 2;
displayxy();
}
Point::Point(int x) {
X = x, Y = 2;
displayxy();
}
Point::Point(int x, int y) {
X = x, Y = y;
displayxy();
}
int main() {
Point p1(3, 4), p2[2] = {5, 6}, p3;
return 0;
}
/*输出结果:
(3,4)
(5,2)
(6,2)
(1,2)
*/
析构函数的用法跟构造函数很像,但要注意调用析构函数的次序和构造函数的次序相反
但是static对象,全局对象可以改变调用次序
拷贝构造函数
默认的复制构造函数
#include<iostream >
using namespace std;
class Complex
{
public:
double real, imag;
Complex(double r, double i) {
real= r; imag = i;
}
};
int main(){
Complex cl(1, 2);
Complex c2 (cl); //用复制构造函数初始化c2
cout<<c2.real<<","<<c2.imag; //输出 1,2
return 0;
}
非默认
#include <iostream>
using namespace std;
class Complex {
public:
double real, imag;
Complex(double r, double i) {
real = r;
imag = i;
}
Complex(const Complex &c) {
real = c.real;
imag = c.imag;
cout << "Copy Constructor called" << endl ;
}
};
int main() {
Complex cl(1, 2);
Complex c2 (cl); //调用复制构造函数
cout << c2.real << "," << c2.imag;
return 0;
}
ppt上的实例
#include <iostream>
using namespace std;
class Point {
public:
Point(int = 0, int = 0);
Point(const Point &); //拷贝构造函数
void displayxy() {
cout << "(" << X << "," << Y << ")" << endl;
}
~Point();
private:
int X, Y;
};
Point::Point(int x, int y) {
X = x, Y = y;
cout << "Constructor is called!";
displayxy();
}
Point::Point(const Point &p) {
X = p.X;
Y = p.Y;
cout << "Copy constructor is called!";
displayxy();
}
Point::~Point() {
cout << "Destructor is called!";
displayxy();
}
void func(Point P) {
int x = 20, y = 20;
Point pp(x, y);
}
int main() {
Point p1(3, 4);
Point p2 = p1;
func(p1);
return 0;
}
/*
Constructor is called!(3,4)
Copy constructor is called!(3,4)
Copy constructor is called!(3,4)
Constructor is called!(20,20)
Destructor is called!(20,20)
Destructor is called!(3,4)
Destructor is called!(3,4)
Destructor is called!(3,4)
*/
课后作业
#include <iostream>
using namespace std;
int x, y, n, m;
class Rectangle {
public:
Rectangle();
Rectangle(int l, int w);//两个构造函数
int getl();
int getw();
int Area();
int Circumference();
void Print();
void Charge(int l, int w);
private:
int len, wid;
};
Rectangle::Rectangle() {
len = 0;
wid = 0;
}
Rectangle::Rectangle(int l, int w) {
len = l;
wid = w;
}
int Rectangle::getl() {
return len;
}
int Rectangle::getw() {
return wid;
}
int Rectangle::Area() {
return wid * len;
}
int Rectangle::Circumference() {
return 2 * (wid + len);
}
void Rectangle::Print() {
cout << "长度:" << getl() << "宽度:" << getw() << "面积:" << Area() << "周长:" << Circumference() << endl;
}
void Rectangle::Charge(int l, int w) {
len = l;
wid = w;
}
int main() {
cout << "请输入矩形长度:";
cin >> x;
cout << "请输入矩形宽度:";
cin >> y;
Rectangle rec1(x, y);
rec1.Print();
cout << "请更改矩形长度:";
cin >> n;
cout << "请更改矩形宽度:";
cin >> m;
rec1.Charge(n, m);
rec1.Print();
return 0;
}
第七讲.构造函数和析构函数
一些关于默认和重载构造函数的区别:
#include <iostream>
using namespace std;
class Student {
public:
//默认形参值构造函数
Student(int i = 1000, int a = 16) {//注意这里的默认值一定要写,否则下面的s1,s2不匹配
id = i;
age = a;
}
//重载构造函数则是写三个,可以不用=默认值
private:
int id, age;
};
int main() {
Student s1, s2(16), s3(1004, 20);
return 0;
}
用参数初始化表对数据成员初始化
在函数首部实现初始化数据成员
作用:对const修饰的数据成员,和引用类型的成员进行赋值
#include <iostream>
using namespace std;
class Demo {
public:
Demo(int b, int a);
void show();
private:
int m_a;
int m_b;
};
Demo::Demo(int b, int a): m_b(b), m_a(a) { }
//参数表的申明顺序与变量赋值的顺序无关
void Demo::show() {
cout << m_a << ", " << m_b << endl;
}
int main() {
Demo obj(100, 200);
obj.show();
return 0;
}
/*
200,100
*/
#include <iostream>
using namespace std;
class Point {
public:
Point(int x = 0, int y = 0) {
X = x, Y = y;
}
void copy(Point &obj);
void displayxy();
private:
int X, Y;
};
void Point::displayxy() {
cout << X << "," << Y << endl;
}
void Point::copy(Point &obj) {
if (this != &obj)
*this = obj;
}
int main() {
//隐式使用
Point obj1(10, 20), obj2(8, 9), *p;
p = &obj1;
p->displayxy();
p = &obj2;
p->displayxy();
//显式使用
Point obj3(10, 20), obj4;
obj4.copy(obj3);
obj4.displayxy();
return 0;
}
/*
10,20
8,9
10,20
*/
对象指针
#include <iostream>
using namespace std;
class Point {
public:
Point(int x = 0, int y = 0) {
X = x, Y = y;
}
void displayxy();
private:
int X, Y;
};
void Point::displayxy() {
cout << X << "," << Y << endl;
}
int main() {
Point A(5, 10);
Point *ptr;
ptr = &A;
int x;
ptr->displayxy();
return 0;
}
/*
5,10
*/
delete和new的用法
#include <iostream>
using namespace std;
class Heapclass {
public:
Heapclass() {}
~Heapclass() {}
private:
int i;
};
int main() {
Heapclass *ptr;
ptr = new Heapclass[2];
if (!ptr) {
cout << "out of Memory!" << endl;
return -1;
}
cout << "Exit main" << endl;
delete[] ptr;
return 0;
}
第八讲 静态&友元
静态成员函数访问类中的非静态数据成员,必须通过参数传递的方式得到对象名,然后通过对象名来访问。
#include <iostream>
using namespace std;
class Point {
public:
Point(int x = 0, int y = 0) {
X = x, Y = y;
}
static void displayxy(Point p);
private:
int X, Y;
};
void Point::displayxy(Point p) {
cout << p.X << "," << p.Y << endl;
}
int main() {
Point A(5, 10);
Point::displayxy(A);
return 0;
}
友元函数和友元类
实例:用教师类看作学生类的友元类
#include <iostream>
#include <cmath>
using namespace std;
class Student {
public:
friend class Teacher;
student() {}
private:
int number, score;
};
class Teacher {
public:
Teacher(int i, int j) {
a.number = i;
a.score = j;
}
void display() {
cout << "NO=" << a.number << " ";
cout << "Score=" << a.score << endl;
}
private:
Student a;
};
int main() {
Student s;
Teacher t1(1001, 89), t2(1002, 78);
t1.display();
t2.display();
return 0;
}
第九讲 继承1
#include <iostream>
using namespace std;
class Point {
public:
Point(int myx, int myy) {
x = myx, y = myy;
}
void displayxy() {
cout << "(" << x << "," << y << ")" << endl;
}
protected:
int x, y;
};
class Circle: public Point {
public:
Circle(int a, int b, int R): Point(a, b) {
r = R;
}
void displayr() {
cout << r << endl;
}
private:
int r;
};
class Cylinder: public Circle {
public:
Cylinder(int a, int b, int R, int H): Circle(a, b, R) {//这个地方也可以在里面写set函数
h = H;
}
void displayh() {
cout << h << endl;
}
private:
int h;
};
int main() {
Cylinder v(4, 8, 2, 10);
v.displayxy();
v.displayr();
v.displayh();
return 0;
}
第十讲 继承2
上面那个例子就是单一继承的构造函数
跟模考考的类似
#include <iostream>
#include <cmath>
using namespace std;
class Student {
public:
Student(int n, string nam) {
num = n;
name = nam;
}
void display() {
cout << "num:" << num << endl;
cout << "name" << name << endl;
}
protected:
int num;
string name;
};
class Student1: public Student {
public:
Student1(int n, string nam, int a): Student(n, nam) {
age = a;
}
void show() {
display();
cout << "age:" << age << endl;
}
protected:
int age;
};
class Student2: public Student1 {
public:
Student2(int n, string nam, int a, int s): Student1(n, nam, a) {
score = s; //对参数进行初始化
}
void show_all() {
show();
cout << "score:" << score << endl;
}
private:
int score;
};
int main() {
Student2 stud(10010, "Li", 17, 89);
stud.show_all();
return 0;
}
多继承
#include <iostream>
using namespace std;
class B1 {
public:
B1(int i) {
cout << i << endl;
}
};
class B2 {
public:
B2(int i) {
cout << i << endl;
}
};
class B3 {
public:
B3() {
cout << "B3" << endl;
}
};
class C: public B2, public B1, public B3 {
public:
C(int a, int b, int c, int d): B1(a), memberB2(d), memberB1(c), B2(b) {
}
private:
B1 memberB1;
B2 memberB2;
B3 memberB3;
};
int main() {
C obj(1, 2, 3, 4);
return 0;
}
/*
2
1
B3
3
4
B3
*/
作业
#include <iostream>
#include <string>
using namespace std;
class Account {
string userName;
public:
Account() {};
Account( string name );
void PrintUserName();
};
class CreditAccount : public Account {
public:
CreditAccount( string name, int credit);
void PrintInfo();
private:
int credit;
};
//请实现Account构造函数Account(string name)
Account::Account(string name) {
userName = name;
}
//请实现Account的PrintUserName()函数
void Account::PrintUserName() {
cout << userName << endl;
}
//请实现CreditAccount类的构造函数
CreditAccount::CreditAccount(string name, int number): Account(name) {
credit = number;
}
//请实现CreditAccount类的PrintInfo()函数
void CreditAccount::PrintInfo() {
Account::PrintUserName();
cout << credit << endl;
}
int main() {
CreditAccount a("I Love CPP", 10000);
a.PrintInfo();
return 0;
}
第十一讲 一些继承的细节
第十二讲、
纯虚函数 const = 0;
第十四讲 运算符重载
++ --的例子
标签:std,复习,Point,int,void,期末,ppt,include,cout 来源: https://www.cnblogs.com/oddpointa/p/16384351.html