qt学习笔记 从c++零基础开始记录

一、C++知识结构

1.1、C++基础

包括基本数据类型,基本逻辑结构if、for、while等简单操作

1.2、面向对象与封装

面向对象编程就是将具有相似属性和方法,但是具体内容不同的的事务封装成类,在进行使用的时候通过实例化成具体的对象进行操作。
例如:
以人为例,每个人都有相同的属性(身高体重、姓名、性别)方法(吃饭、休息),但是具体的身高体重、姓名、性别以及吃什么、休息多久是不同的。
main.cpp:

#include <iostream>
#include "people.cpp"

using namespace std;
int main(){
   
   
    People worker;//实例化一个worker 
    People student;//实例化一个student

    // 赋予worker他的属性 
    worker.name = "张三" ;
    worker.sex = "男"; 
    worker.height = 178;
    worker.weight =70;

    // 输出worker的基本属性
    cout<<worker.name<<" 性别:"<<worker.sex<<"  身高:"<<worker.height<<" 体重:"<<worker.weight<<endl;; 
    // 执行 worker的基本内容 
    worker.eat("面包");
    worker.work(8);

    cout<<"************************************************************************************************"<<endl; 
    // 赋予student他的属性 
    student.name = "李四" ;
    student.sex = "女"; 
    student.height = 165;
    student.weight =50;

    // 输出worker的基本属性
    cout<<student.name<<" 性别:"<<student.sex<<"  身高:"<<student.height<<" 体重:"<<student.weight<<endl;; 
    // 执行 worker的基本内容
    worker.eat("零食");
    worker.work(3);
    return 1;
}

people.cpp

#include <string>
#include <iostream> 
using namespace std;
class People{
   
   
    public:
        string name;
        string sex;
        int height;
        int weight;
    public:
        void eat(string food){
   
   
            cout<<"吃了"<<food<<endl;
        };
        void work(int time){
   
   
            cout<<"干了"<<time<<"小时工作"<<endl;

        };
};

1.3、继承与多态

子类继承父类,但是父类并不一定拥有子类所需要的所有方法,因此子类需要增加自己的方法,那么新增的方法如何访问其他数据内容?

1.4、类访问修饰符

通过进行面向对象形式的封装结合类访问修饰符,在访问类的时候无法直接获取数据,而只能通过调用类的方法获取数据(获取固定内容的数据)

#include <string>
#include <iostream> 
using namespace std;
class People{
   
   
	private:
		string name;
		string sex;
		int height;
		int weight;
	public:
		void setName(string name){
   
   
			this->name =  name;
		}
		void setSex(string sex){
   
   
			this->sex =  sex;
		}

		void setHeight(int height){
   
   
			this->height =  height;
		}
		void setWeight(int weight){
   
   
			this->weight =  weight;
		}
		string getName(){
   
   
			return this->name;
		}
		string getSex(){
   
   
			return this->sex;
		}
		int getHeight(){
   
   
			return this->height;
		}
		int getWeight(){
   
   
			return this->weight;
		}

		void eat(string food){
   
   
			cout<<"吃了"<<food<<endl;
		};
		void work(int time){
   
   
				cout<<"干了"<<time<<"小时工作"<<endl;
		};
};

那么main函数设置内容或者获取内容的时候,只能通过set和get进行设置和获取。

#include <iostream>
#include "people.cpp"

using namespace std;
int main(){
   
   
	People worker;//实例化一个worker 
	
	// 赋予worker他的属性 
	worker.setName("张三");
	worker.setSex("男");
	worker.setHeight(178);
	worke
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值