namespace

97 篇文章 0 订阅

1 person.h

#ifndef PERSON_H
#define PERSON_H

#include<string>
#include<iostream>
using namespace std;
namespace stdperson
{
	class Person
	{
	public:
		Person();
		Person(string name, int age);
		void SetName(string name);
		void SetAge(int age);
		string GetName();
		int    GetAge();

	private:
		string name;
		int age;
	};
}

#endif	PERSON_H

2 person.cpp

#include"person.h"
using namespace stdperson;
Person::Person()
{
	SetName("默认值");
	SetAge(0);
}
//--------------------------------------------------------------------------
Person::Person(string name, int age)
{
	SetName(name);
	SetAge(age);
}
//---------------------------------------------------------------------------
void Person::SetName(string name)
{
	this->name = name;
}
//---------------------------------------------------------------------------
void Person::SetAge(int age)
{
	this->age = age;
}

//---------------------------------------------------------------------------
string Person::GetName()
{
	return this->name;
}
//---------------------------------------------------------------------------
int Person::GetAge()
{
	return this->age;
}
//----------------------------------------------------------------------------

3 student.h

#ifndef	STUDENT_H
#define STUDENT_H

#include "person.h"
using namespace stdperson;

namespace stdstudent
{
	class Student : public Person
	{
	public:
		Student(string name, int age, string schoolName);
		Student();
		string GetSchoolName();
		void   SetSchoolName(string schoolName);

	private:
		string schoolName;
	};
}

#endif

4 student.cpp

#include "student.h"
using namespace stdstudent;

Student::Student(string name, int age, string schoolName)
:Person(name, age),schoolName(schoolName)
{
}

//---------------------------------------------------------------
Student::Student()
:Person()
{
}

//----------------------------------------------------------------
void Student::SetSchoolName(string schoolName)
{
	this->schoolName =  schoolName;
}

//----------------------------------------------------------------
string Student::GetSchoolName()
{
	return this->schoolName;
}

5 main.cpp

#include"person.h"
#include"student.h"
using namespace stdperson;
using namespace stdstudent;


int main()
{
	Person p("张三", 22);

	Student s("里斯", 23, "香港大学");
	
	cout<<p.GetName()<<endl;
	cout<<p.GetAge()<<endl;
	cout<<"**************************************************"<<endl;
	
	cout<<s.GetName()<<endl;
	cout<<s.GetAge()<<endl;
	cout<<s.GetSchoolName()<<endl;
	system("pause");
	return 0;
}

6运行结果


例2

1 read.h

#ifndef READ_H
#define READ_H
#include<fstream>
#include<string>
#include<iostream>
using namespace std;

namespace stdread
{
	void ReadToger(ifstream& ifs, string word);
	void SayHello();
}
#endif

2 read.cpp

#include "read.h"
using namespace stdread;
void stdread::ReadToger(ifstream& ifs, string word)
{
	while(ifs>>word)
	{
		cout<<word<<endl;
	}

}

void stdread::SayHello()
{
	cout<<"hello, welcome to here!"<<endl;
}

3 main.cpp

#include "read.h"
using namespace stdread;
int main()
{
	
	ifstream ifs("data\\script\\fishname.txt");
	string word;

	ReadToger(ifs, word);

	
	system("pause");
	return 0;
}

data是相对了工程目录的,用流做参数,参数要是reference

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值