C++面向对象编程学生类案例

C++面向对象编程,学生类案例

采用引用std简化

cpp 复制代码
#include <string.h>
#include <iostream>
using namespace std;

class Student{
	private:
		string name ;
		int age;
		string classname ;
	public:
		Student(string name,int age,string clsname ){
			this->age=age;
			this->name=name;
			this->classname=clsname;
		};
		Student(){
			this->age=18;
			this->name="susan";
			this->classname="2331";
		};
	
		void setName(string name){
			 this->name=name;
		};
		void show(){
		  cout<<"student name is "<<this->name+" classname"<<this->classname<<endl;
		};
		string getName(){
		   return this->name;	
		};
		
};

int main(){
	
	Student *s,s1("Lily",20,"2031");
   	s=new Student("Xiaoming",21,"2231");
    s->show();
    s= &s1;
    s->show();    
	s1.show();
	cout<< s->getName();
				
}
		
		
cpp 复制代码
//方案1,std采用静态类调用
#include <string.h>
#include <iostream>


class Student{
	private:
		std::string name ;
		int age;
		std::string classname ;
	public:
		Student(std::string name,int age,std::string clsname ){
			this->age=age;
			this->name=name;
			this->classname=clsname;
		};
		Student(){
			this->age=18;
			this->name="susan";
			this->classname="2331";
		};
	
		void setName(std::string name){
			 this->name=name;
		};
		void show(){
		  std::cout<<"student name is "<<this->name+" classname"<<this->classname<<std::endl;
		};
		std::string getName(){
		   return this->name;	
		};
		
};

int main(){
	
	Student *s,s1("Lily",20,"2031");
   	s=new Student;
    s->show();
    s= &s1;
    s->show();    
	s1.show();
	std::cout<< s->getName();
				
}
		
		

运行结果

相关推荐
lsx20240617 小时前
Bootstrap 标签详解
开发语言
FL162386312917 小时前
Qt自定义控件之仪表盘和水波纹圆形进度条的完整实现
开发语言·qt
缺点内向17 小时前
Java: 在 Excel 中插入、提取或删除文本框
java·开发语言·excel
xlq2232218 小时前
16.17.list(上)
c++·list
星释18 小时前
Rust 练习册 60:鲍勃与字符串处理的状态机思维
开发语言·网络·rust
淡淡蓝蓝18 小时前
uni.uploadFile使用PUT方法上传图片
开发语言·前端·javascript
PyHaVolask18 小时前
PHP基础入门
开发语言·php
cpp_250118 小时前
P1765 手机
数据结构·c++·算法·题解·洛谷
未到结局,焉知生死18 小时前
PAT每日三题11-20
c++·算法
乘乘凉18 小时前
C#中的值传递和引用传递
java·开发语言·c#