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();
				
}
		
		

运行结果

相关推荐
ajassi20002 分钟前
开源 C++ QT QML 开发(十二)通讯--TCP客户端
c++·qt·开源
进击的圆儿40 分钟前
【学习笔记05】C++11新特性学习总结(下)
c++·笔记·学习
dlraba80243 分钟前
用 Python+OpenCV 实现实时文档扫描:从摄像头捕捉到透视矫正全流程
开发语言·python·opencv
Jayden_Ruan1 小时前
C++十进制转二进制
数据结构·c++·算法
一人の梅雨1 小时前
1688 店铺商品全量采集与智能分析:从接口调用到供应链数据挖掘
开发语言·python·php
小何好运暴富开心幸福1 小时前
C++之日期类的实现
开发语言·c++·git·bash
威风的虫2 小时前
JavaScript中的axios
开发语言·javascript·ecmascript
老赵的博客2 小时前
c++ 是静态编译语言
开发语言·c++
Terio_my2 小时前
Python制作12306查票工具:从零构建铁路购票信息查询系统
开发语言·python·microsoft
消失的旧时光-19432 小时前
Kotlin when 用法完整分享
android·开发语言·kotlin