【星海出品】C++的基础(二)(通过代码学习)

继承

公有继承为公有
私有继承为私有

一个学员的信息,保存到一个 txt 文件中。

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class Student{
	public:
		int num;
		char name[20];
		int core;
	friend void save();
	friend void view();
};

void view(Student *s2){
	cout << s2->num << endl;
	cout << s2->name << endl;
	cout << s2->core << endl;
}

void save(Student *s2){

	ofstream destFile("out.txt",ios::in);//以文本模式打开in.txt
	if(!destFile){ //打开失败 
		cout << "error opening destination file." << endl; 
	}
	destFile << s2->num << " ";
	destFile << s2->name << " ";
	destFile << s2->core << " ";
	
	destFile.close();
	
	/*
	ofstream fp("F:\\socre.txt",ios:in);
	if(!fp){
		cout << "Can't open the file" << endl;
	}
	for(i=0;i<1,i++)
		fp.write((const char *)&stu[i],sizeof(Student));
		fp.close();
	*/
}

main(){
	Student s1;
	cout << "please input you studentNum: " << endl;
	cin >> s1.num;
	cout << "you studentNum is : " << s1.num << endl;
	
	cout << "please input you name" << endl;
	cin >> s1.name;
	cout << "you name is :" << s1.name << endl;
	
	cout << "please input you core : " << endl;
	cin >> s1.core;
	cout << "you core is : " << s1.core << endl;
	
	view(&s1);
	save(&s1);

}

掌握继承与派生的使用方法,掌握继承中的构造函数与析构函数的调用顺序,为派生类设计合适的构造函数初始化派生类;理解多继承的概念和编程,

C++的钻石继承

#include <iostream>
using namespace std;
class A{
	int a;
	public:
		A(int n){
			a=n;
			cout << "A::a=" << a << endl;
		}
		~A(){
			cout << "A的对象在消亡" << endl; 
		};
};

class B{
	int b;
	public:
		B(int n1,int n2){
			b=n1;
			cout << "B::b=" << b << endl;
		}
		~B(){
			cout << "B的对象在消亡" << endl; 
	    };
};

class C:public A,public B{
	int c;
	public:
		C(int n1,int n2, int n3,int n4):B(n3,n4),A(n2){
			c=n1;
			cout << "C::c=" << c << endl;
		}
		~C(){
			cout << "C的对象在消亡" << endl; 
		};
};

int main(){
	C Cobj(1,3,5,7);
	return 0;
}

结果展示:
A::a=3
B::b=5
C::c=1
C的对象在消亡
B的对象在消亡
A的对象在消亡

C++ 类模板

//类模板中成员函数的创建时机
#include <iostream>
using namespace std;

class Person1
{
public:
	void showPerson1()
	{
		cout << "Person1 show" << endl;
	}
};

class Person2
{
public:
	void showPerson2()
	{
		cout << "Person2 show" << endl;
	}
};

template<class T>
class Myclass
{
public:
	T obj;
	
	//类模板中的成员函数在调用的时候才创建,所以不会报错
	void func1()
	{
		obj.showPerson1();
	}

	void func2()
	{
		obj.showPerson2();
	}
};

void test01()
{
	Myclass<Person1>m;
	m.func1();
	//m.func2(); 无法调用
};

int main()
{
	test01();
	system("pause");
	return 0;
}

结果: Person1 show
类模板的声明还可以是template<class S1,class S2>

#include <iostream>
using namespace std;
class Test{
	private:
		int n;
	public:
		Test(int i=16){
			n=--i;
		}
		int Get(){
			return n;
		}
		int Get()const {
			return(n-2);
		}
};
 
int main(){
	Test a;
	const Test b(27);
	cout << a.Get() << endl;
	cout << b.Get() << endl;
	return 0; 
}

结果 15 24

简单的大小排序

#include <iostream>
using namespace std;

template <typename T>
int sort(const T&l,const T&r){
	if(l<r){
		return -1;
	}
	else{
		cout << l << r << endl;
		return 1;
	}
}

template <class T>
void sw(T &x,T &y){
	T t;
	t=x;
	x=y;
	y=t;
}

int main(){
	int a[4] = {1,3,12,4};
	for(int i=1;i<4;i++)
	{
		int j=i;
		while(j>0&&sort<int>(a[j-1],a[j])>0){
			sw(a[j],a[j-1]);
			j--; 
		}
	}
	cout << "pass" << endl;
	for(int i=0;i<4;i++){
		cout<<a[i]<<' ';
	}
	return 0;
}

static的使用

#include <iostream>
using namespace std;

class Cat {
public:
    Cat() { numOfCats++; }

    static void getNumOfCats();

private:
    static int numOfCats;
};

int Cat::numOfCats = 0;

void Cat::getNumOfCats() {
    cout << "猫的数量:" << numOfCats << endl;
}

int main() {
    Cat::getNumOfCats();    //static函数属于类,并不属于某个对象,所以可以这样调用,当然通过对象也可以调用
    Cat cat;
    Cat::getNumOfCats();
    //cat.getNumOfCats();
    Cat cat2;
    cat.getNumOfCats();
}
类的定义。
#include<iostream>

using namespace std;

class Num
{
	public:
		void input(int m,int n);
		void output();
	private:
		int num1;
		int num2;
};

void Num::input(int m,int n)   //域操作符 ::
{
	num1=m;
	num2=n;
} 

void Num::output()       
{
	cout<<num1<<" "<<num2<<endl; 
}

int main()
{
	Num M;
	M.input(5,10);
	M.output() ;
	return 0;  
}

根据类创建各种对象。

	Num M;
	Num allN[100];
	for(int i = 0; i < 10; i++){
		allN[i+1].input(i,i+1);
		allN[i+1].output();
	}

掌握对象的各种成员的使用,通过定义构造函数实现对象的初始化。

class Person
{
public:
	Person()
	{
		name = "张三";
		age = 18;
	}
	string name;
	int age;
};
#include <iostream>
#include <string.h>
using namespace std;

class Person
{
public:
	Person(string name, int age)
	{
		this->age = age;
		this->name = name;
	}

    Person(const Person& P)
	{
		this->age = P.age;
		this->name = P.name;
	}
	
	string name;
	int age;
	void Out();
};

void Person::Out(){
	cout << age << endl;
}

int main(){
	Person a("a",2);
	a.Out();
	Person P3(a);
	P3.Out();
	return 0;
}

虚继承的目的是让某个类做出声明,承诺愿意共享它的基类。其中,这个被共享的基类就称为虚基类(Virtual Base Class),本例中的 A 就是一个虚基类。在这种机制下,不论虚基类在继承体系中出现了多少次,在派生类中都只包含一份虚基类的成员。

//间接基类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<iostream>
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。

利用虚函数;虚函数和抽象类的定义。
需要注意:抽象类虽然不能创建自己的对象,但是可以有自己的指针

class controller
{
	public:
		controller();
		void func() = 0; // 纯虚函数,意味着这是一个抽象类
	protected:
		int x;
		int y;
};
int main()
{
	controller MPCController; //错误,抽象类不能有自己的对象
	controller *p_controller; //正确,抽象类可以有自己的指针
}

输入输出流,读写文本文件和二进制文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值