【星海随笔】C++程序设计(实践)04738复习资料

目录

1.集成开发工具的使用(新建项目、新建类、修改与运行程序)
2.C++程序的基本结构与基础语法(类、对象的定义、构造函数的建立)
3.常见的C++程序的输入、输出语句
4.运算符的重载
5.多态性与虚函数的使用
6.文件管理与操作

1.掌握类的定义,根据类创建各种对象,掌握对象的各种成员的使用方法,通过定义构造函数实现对象的初始化。
2.掌握继承与派生的使用方法,掌握继承中的构造函数与析构函数的调用顺序,为派生类设计合适的构造函数初始化派生类;理解多继承的概念和编程,理解虚基类的概念,掌握虚基类的使用方法。
3.理解多态性的概念,掌握如何使用虚函数实现动态联编,掌握如何利用虚函数;理解纯虚函数和抽象类的概念,掌握纯虚函数和抽象类的定义方法。
4.理解运算符重载的概念;掌握运算符重载的规则,能够对一般的运算符进行重载。
5.理解输入/输出流概念,掌握读、写、便利文本文件和二进制文件。

备注:
自考题库
https://wangxiao.xisaiwang.com/tiku2/list-zt1517-1.html?areaName=

默认情况下eclipse中是不会安装gcc和g++编译器的,需要用户手动安装。
http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/

C++程序的基本结构与基础语法(类、对象的定义、构造函数的建立)

构造类、以及对象的建立、构造函数的建立
#include 
using namespace std;

class Student{
	int Num=0,Score=0;
	
	public:
		Student(int a,int b){
			Num=a;
			Score=b; 
			cCount++;
		}
		void get(){cout << Num << "." << Score << endl;}
		static int cCount; //得到静态成员变量;
		void Sget(){
			cout << cCount << endl; 
		} 
};

int Student::cCount = 0;

int main(void){
	Student studentO(2,2);
	
	Student student(1,96);
	student.get();
	student.Sget();
		
	stuT = new Student(3,3);
	stuT->get();
	delete stuT;
	//Studeng *b = new Student();
	//Student *b;  只调取类指针。
	//b = new Student();  new 
	//b->setName("B");
	return 0;
}
#include 
using namespace std;
class A{
private:
	int m;
public:
	A(int i=0){
		m=i;
		cout<<"constructor called." << m << "ln";
	}
	
	void Set(int i){m=i;}
	
	void Print()const{cout << '\n' << m << endl;}
	~A(){cout << "destructor called." << m << "ln";
	}
};

void fun(const A&c){c.Print();}
int main(void){
	fun(5);
}

常见的C++输入输出
#include 
#include  
using namespace std;

int a[] = {2,4,6,7,10};
int &index(int i)
{
	return a[i];
}

int main(void){
	//索引选择 
	int i;
	cout << index(3)<< endl;
	index(3) = 8;
	cout << index(3) << endl;
	
	//自定义型,全部打印出来 
	for(int i=0; i<5; i++){
		cout << a[i] << " ";
	}
	
	//ASCII 码相加 
	char c = 'a' + 20;
	cout.put('\n');
	cout.put(c);
	cout.put('\n');
	
	//数学问题
	int f,t1;
	cout << f-32 << endl;
	//result is -32
	t1 = 3* (f-32) / 2;
	cout << t1 << endl; 
	
	return 0;
}
运算符重载

运算符重载格式

<ClassName> operator+(<className> & p)
ostream& operator<<(ostream& cout, <ClassName>& p)
bool operator > (<ClassName> & p)
<ClassName> & operator++()
<ClassName> operator++(int)
<ClassName> &operator=(<ClassName> & p)
class Person {
public:
	//成员函数重载+号,本质调用为Person p3 = p1.operator+(p2);
	Person operator+(Person& p)
	{
		Person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_A + p.m_B;
		return temp;
	}

	int m_A;
	int m_B;
};
多态性与虚函数的使用
//间接基类A
class A{
protected:
    int m_a;
};
//直接基类B
class B: virtual public A{  //虚继承
protected:
    int m_b;
};
//直接基类C
class C: virtual public A{  //虚继承
protected:
    int m_c;
};
//派生类D
class D: public B, public C{
public:
    void seta(int a){ m_a = a; }  //正确
    void setb(int b){ m_b = b; }  //正确
    void setc(int c){ m_c = c; }  //正确
    void setd(int d){ m_d = d; }  //正确
private:
    int m_d;
};
int main(){
    D d;
    return 0;
}
#include
using namespace std;
class Animal {
public:
    virtual void speak() {
        cout << "动物在说话" << endl;
    }
};

class Cat :public Animal {
public:
    void speak() {
        cout << "小猫在说话" << endl;
    }
};
class Dog :public Animal {
public:
    void speak() {
        cout << "小狗在说话" << endl;
    }
};

void doSpeak(Animal &animal) {
    animal.speak();
}

void test01() {
    Cat cat;
    doSpeak(cat);
    Dog dog;
    doSpeak(dog);
}

int main() {
    test01();
    system("pause");
    return 0;
}
//如果输出sizeof(Animal)结果为4字节,Animal类中存在一个虚函数
//表指针vfptr,vfptr指向一个虚函数表,表中记录虚函数的入口地址
//&Animal::speak,当子类重写父类的虚函数时,子类虚函数表中的内容
//改变为&Cat::speak。
文件管理与操作

写文件

#include
#include
using namespace std;
#include    //头文件包含
 
//文本文件 - 写文件
 
void text01()
{
    //1.包含头文件 fstream
 
    //2.创建流对象
    ofstream ofs;        // o - 写 f - 文件 stream - 流
 
    //3.指定打开方式
    ofs.open("text.txt", ios::out);
 
    //4.写内容
    ofs << " 姓名:张三" << endl;
    ofs << " 年龄:19 " << endl;
    ofs << " 性别:男 " << endl;
 
    //5.关闭文件
    ofs.close();
 
}
int main()
{
    text01();
    return 0;
}

读文件

void text02()
{
    //1.包含头文件 fstream
 
    //2.创建流对象
    ifstream ifs;            //ifstream ifs("Person.txt", ios::out | ios::binary);
 
    //3.打开文件 并且判断是否打开成功
    ifs.open("text.txt", ios::in | ios::binary);
    if (!ifs.is_open())
    {
        cout << "文件打开失败" << endl;
        return;
    }
 
    //4.读内容
 	for (int i = 0; i < 5 ; i++ ){
	
 		char str[100] = { 0 };
 		ifs.getline(str, 100);
 		std::cout << str << std::endl;
 	}

    //5.关闭文件
    ifs.close();
}

判断文件行数

#include 

int main()
{
    FILE * fp = NULL; //文件指针。
    int c, lc=0; //c为文件当前字符,lc为上一个字符,供结尾判断用。
    int line = 0; //行数统计
    fp = fopen("text.txt", "r");//以只读方式打开文件。
    if(fp == NULL) return -1; // 文件打开失败。
    while((c = fgetc(fp)) != EOF) //逐个读入字符直到文件结尾
    {
        if(c == '\n') line ++; //统计行数。
        lc = c; //保存上一字符。
    }
    fclose(fp); //关闭文件
    if(lc != '\n') line ++;//处理末行
     
    printf("文件共有%d行。\n", line);
     
    return 0;
}
————————————————
版权声明:本文为CSDN博主「活跃的煤矿打工人」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_41997073/article/details/126856692

附录:

**类的定义,属性,方法,抽象等等。**
- class {};

**类的继承。**
- class A : public B {}; 
- class B: virtual public A{};
- class D: public B, public C{};

**对象的定义和使用。**
- *p = new   //进行初始化,才占用真实的内存;
-  *P   //不占用真实的内存;


**分支结构、循环结构**
#include 
using namespace std;

class A{
	private:
		int tt=0;
	
	public:
		A(int a){
			int tt = a;
		}
};

void Qsort(int arr[], int low, int high){
    if (high <= low) return;
    int i = low;
    int j = high;
    int key = arr[low];
    
    while (true)
    {
        /*从左向右找比key大的值*/
        while (arr[i] <= key)
        {
            i++;
            if (i == high){
                break;
            }
        }
        /*从右向左找比key小的值*/
        while (arr[j] >= key)
        {
            j--;
            if (j == low){
                break;
            }
        }
        if (i >= j) break;
        /*交换i,j对应的值*/
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    /*中枢值与j对应值交换*/
    arr[low] = arr[j];
    arr[j] = key;
    Qsort(arr, low, j - 1);
    Qsort(arr, j + 1, high);
}

int main()
{
    int a[] = {57, 68, 59, 52, 72, 28, 96, 33, 24};
 
    Qsort(a, 0, sizeof(a) / sizeof(a[0]) - 1);/*这里原文第三个参数要减1否则内存越界*/

    for(int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
        {
        cout << a[i] << " ";
    }
    
    return 0;
}/*参考数据结构p274(清华大学出版社,严蔚敏)*/

**输入、输出**
#include 
using namespace std;

int main()
{
    char a;
    int b;
    float c;
	cout << "please input a" << endl;
    cin >> a ;
    cout << "please input b" << endl;
	cin >> b ;
	cout << "please input c" << endl;
	cin >> c ;
	cout << "ok"<< endl;
    cout<<a<<" "<<b<<" "<<c<<" "<<endl;
    system("pause");
    return 0;
}

**数学运算**
/*
* Author:W;
* 常用的数学运算
*/
 
//引入头文件:头文件包含了程序中必需的或有用的信息【单行注释】
#include 
//引入数学计算库文件
#include 
//命名空间使用
using namespace std;
 
 
//main程序执行入口函数
int main()
{
	double angle = 235.55f;
	double v1 = 2.5f;
	double v2 = 3.5f;
	int num = -199;
	int num2 = 255;
 
	//余弦
	cout << "cos(" << angle << ") = "<< cos(angle) << endl;
	//正弦
	cout << "sin(" << angle << ") = " << sin(angle) << endl;
	//正切
	cout << "tan(" << angle << ") = " << tan(angle) << endl;
	//自然对数
	cout << "log(" << v1 << ") = " << log(v1) << endl;
	//x的y次方
	cout << "pow(" << num << "," << v1 << ") = " << pow(num, v1) << endl;
	//平方总和的平方根
	cout << "hypot(" << v1 << "," << v2 << ") = " << hypot(v1,v2) << endl;
	//平方根
	cout << "sqrt(" << num2 << ") = " << sqrt(num2) << endl;
	//整数的绝对值
	cout << "abs(" << num << ") = " << abs(num) << endl;
	//浮点数的绝对值
	cout << "fabs(" << v1 << ") = " << fabs(v1) << endl;
	//小于或等于传入值的最大整数
	cout << "floor(" << v2 << ") = " << floor(v2) << endl;
 
}
#include 
using namespace std;
 
int main()
{
   int a = 21;
   int b = 10;
   int c;
 
   c = a + b;
   cout << "Line 1 - c 的值是 " << c << endl ;
   c = a - b;
   cout << "Line 2 - c 的值是 " << c << endl ;
   c = a * b;
   cout << "Line 3 - c 的值是 " << c << endl ;
   c = a / b;
   cout << "Line 4 - c 的值是 " << c << endl ;
   c = a % b;
   cout << "Line 5 - c 的值是 " << c << endl ;
 
   int d = 10;   //  测试自增、自减
   c = ++d;
   cout << "Line 6 - c 的值是 " << c << endl ;

   d = 10;    // 重新赋值
   c = --d;
   cout << "Line 7 - c 的值是 " << c << endl ;
   d--;
   cout << d <<endl;
   return 0;
}

字符串操作

#include 
#include 
using namespace std;

int main()
{
	string s = "hello world";
	cout << s << endl;
	//string::size_type
	for ( int ix = 0; ix != s.size(); ++ix)
		s[ix] = '*';
    	cout<<"Now s is:"<<s<<  endl;

		cout<<"s's len is:"<<s.size()<<", s[11]="<<s[11]<<  endl;

	string pos;

	int len = 5;
	string s1;    // 初始化一个空字符串
    string s2 = s1;   // 初始化s2,并用s1初始化
    string s3(s2);    // 作用同上
    string s4 = "hello world";   // 用 "hello world" 初始化 s4,除了最后的空字符外其他都拷贝到s4中
    string s5("hello world");    // 作用同上
    string s6(6,'a');  // 初始化s6为:aaaaaa

    string s7(s5, 0);  // s7 是从 s6 的下标 3 开始的字符拷贝

    cout << s7 << endl;
	//string s8(s6, pos, len);  // s7 是从 s6 的下标 pos 开始的 len 个字符的拷贝

    return 0;
}

扩展指针

#include 
#include 
using namespace std;

class animal{
	private:
		string name = "animal";
	public:
		int high = 0;
		int weight = 0;
		void setStatus(int a,int b){
			high = a;
			weight = b;
		}
		virtual void setName(string a) = 0;
		virtual void toldName() = 0;
};


class climb:public animal{
	public:
		string name;
		void setName(string a){
			name = a;
		};
		void pringStatus(){
			cout << "animal high is: " <<  high << endl;
			cout << "animal weight is: " <<  weight << endl;
		};
		void toldName(){
			cout << name << endl;
		};
};


int main()
{
	cout << "C++ Test project" << endl;
	climb AnOne;
	int a = 2;
	int b = 5;
	AnOne.setName("aha");
	AnOne.toldName();
	AnOne.setStatus(a, b);
	AnOne.pringStatus();

	animal *ptrt[2];

	ptrt[0] = &AnOne;
	ptrt[0]->toldName();
	ptrt[0]->pringStatus(); // flase

	cout <<"end" << endl;
	return 0;
}
//============================================================================
// Name        : TEST20230202.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include 
#include 
using namespace std;


class animal{
	private:
		string name = "animal";
	public:
		int high = 0;
		int weight = 0;
		void setStatus(int a,int b){
			high = a;
			weight = b;
		}
		virtual void setName(string a) = 0;
		virtual void toldName() = 0;
};


class climb:public animal{
	public:
		string name;
		void setName(string a){
			name = a;
		};
		void pringStatus(){
			cout << "animal high is: " <<  high << endl;
			cout << "animal weight is: " <<  weight << endl;
		};
		void toldName(){
			cout << name << endl;
		};
};


class fly:public animal{
	public:
		string name;
		void setName(string a){
			name = a;
		};
		void pringStatus(){
			cout << "animal high is: " <<  high << endl;
			cout << "animal weight is: " <<  weight << endl;
		};
		void toldName(){
			cout << name << endl;
		};
};


int main()
{
	cout << "C++ Test project" << endl;
	cout << "TEST" << endl;

	climb AnOne;
	fly TwoOne;
	int a = 2;
	int b = 5;

	AnOne.setName("aha");
	AnOne.toldName();

	TwoOne.setName("oo");
	TwoOne.toldName();

	AnOne.setStatus(a, b);
	AnOne.pringStatus();

	TwoOne.setStatus(a+1,b+2);
	TwoOne.pringStatus();

	AnOne.pringStatus();
	animal *ptrt[2];

	ptrt[0] = &AnOne;
	ptrt[0]->toldName();
	//ptrt[0]->pringStatus(); // flase

	ptrt[1] = &TwoOne;
	ptrt[1]->toldName();

	cout <<"end" << endl;
	return 0;
}

#include 
#include 
using namespace std;

class Base{
	public:
		virtual ~Base(){}
		virtual void className() = 0;
	protected:
		string name;
};


class A:public Base{
	private:
		string a;
	public:
		A(){}
		~A(){}
		A(string name):a(name){}
	public:
		void set(string name){
			a = name;
		}
	    string get()const{
	    	return a;
	    }
	    void className() override{
	    	//如果派生类在虚函数声明时使用了override描述符,那么该函数必须重载其基类中的同名函数,否则代码将无法通过编译。
	    	std::cout << "instance A name: " << a.c_str() << endl;
	    }
};


class B :public Base{
	private:
    	string b;
	public:
		B(string name) :b(name){}
		B(){}
		~B(){}
	public:
		void set(string name){
			b = name;
		}
		string get()const{
			return b;
		}
	    void className() override{
	    	std::cout << "instance B name: " << b.c_str() << endl;
	    }
};


class C :public Base{
	private:
		string c;

	public:
    	C() {}
    	C(string name) : c(name){}
    	~C() {}
	public:
    	void set(string name){
    		c = name;
    	}
    	string get() const{
    		return c;
    	}
    	void className() override{
    		cout << "instance C name: " << c.c_str() << endl;
    	}
};

int main() {
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
	Base* arr[] = {
			new A("xiaozhu"),new B("niao"),new C("chongzi")
	};

	for (int i = 0, size = sizeof(arr)/sizeof(arr[0]); i < size; i++){
		Base* base = arr[i];
		base->className();
		delete base;
	}

}



你可能感兴趣的:(C++,linux,计算机基础,c++,算法,开发语言)