C++语言程序设计(西电版)第7章 类与对象 代码

本文详细介绍了C++编程中的主程序组织、头文件的使用,包括时间类(如时钟和日期)、学生类(涉及构造函数、析构函数和对象操作)以及tinyxml的命名空间和基本用法。通过实例展示了如何创建和操作类对象,以及参数传递的方式。
摘要由CSDN通过智能技术生成

本章开始引入头文件,所以方便起见直接编辑了一个主函数和一堆头文件。

这回主函数是顺序排列的,都用/*和*/分开了,需要运行某一段调整一下注释就行

需要请自取,可能会补充一些

目录

一. 主程序

二.头文件

2.1

2.1.1 Clock_1.h

2.1.2 Clock_2.h

2.2

2.2.1 Tdate_1

2.2.2 Tdate_2

2.2.3 Tdate_3

2.3

2.3.1 Student_1.h

2.3.2 Student_2.h

2.3.3 Student_3.h

2.3.4 Student_4.h

2.4

2.4.1 tinyxml.h


一. 主程序

*//*

//7-1.1定义一个简单时钟 
#include"Clock_1.h"
int main()
{
	Clock myclock;
	myclock.SetTime(12,5,0);
	myclock.ShowTime();
	myclock.ShowTime(1);
} 

*//*

//7-1.2定义一个有构造函数的时钟 
#include"Clock_2.h"
int main()
{
	Clock myclock(8,30,0);
	myclock.ShowTime();
	
	myclock.SetTime(15,38,24);
	myclock.ShowTime();
} 


*//*

//7-2构造函数的重载
#include"Tdate_1.h"
int main()
{
	Tdate aday;
	Tdate bday(10);
	Tdate cday(2,12);
	Tdate dday(1,2,1998);
}

*//* 

//7-3带默认参数值的构造函数 
#include"Tdate_2.h"
int main()
{
	Tdate aday;
	Tdate bday(10);
	Tdate cday(2,12);
	Tdate dday(1,2,1998);
}

*//* 

//7-4 定义析构函数,复制构造函数 
#include"Student_1.h"
int main()
{
	student stu(1,"wang",18,86);
	student stu2(stu);
	
	stu.printstu();
	stu2.printstu();
} 

*//*

//7-5使用对象数组,但无初始化,使用默认构造函数 
#include"Student_2.h"
int main()
{
	student stu[5];
	stu[0].set(1, "Wang", 18, 86);
	stu[1].set(2, "Li  ", 18, 72);	
	stu[2].set(3, "Zhao", 18, 80);	
	stu[3].set(4, "Guo ", 18, 85);	
	stu[4].set(5, "Meng", 18, 75);	
	for(int i = 0; i<5; i++)
	    stu[i].printstu();
}

*//* 

//7-6 使用构造函数和析构函数,有初始化,使用对象数组 
#include"Student_3.h"
int main()
{
	student stu[5] = 
	{
	student(1, "Wang", 18, 86),
	student(2, "Li  ", 18, 72),	
	student(3, "Zhao", 18, 80),	
	student(4, "Guo ", 18, 85),	
	student(5, "Meng", 18, 75)		
	};

	for(int i = 0; i<5; i++)
	    stu[i].printstu();
}

*//* 

//7-7 结合7-5、7-6两种方法,使用对象数组 
#include"Student_4.h"
int main()
{
	student stu[5] = 
	{
	student(1, "Wang", 18, 86),
	student(2, "Li  ", 18, 72),	
	student(3, "Zhao", 18, 80),		
	};
	
	stu[3].set(4, "Guo ", 18, 85);	
	stu[4].set(5, "Meng", 18, 75);		

	for(int i = 0; i<5; i++)
	    stu[i].printstu();
}

*//* 

//7-8 在主程序中定义一个类对象,将其作为参数传递给普通函数 
#include"Tdate_3.h"
#include<iostream>
using namespace std;

void someFunc(Tdate someday) 
{
	someday.Print();
	if(someday.IsLeapYear()) 
	      cout<<"leap year\n";
	else  cout<<"not leap year\n";
}

int main()
{
	Tdate s;
	int m,d,y;
	cout<<"请输入月 日 年:"<<endl;
	cin>>m>>d>>y;
	s.Set(m,d,y);
	someFunc(s);	
}

*//*

//
#include"Tdate_3.h"
#include<iostream>
using namespace std;

void someFunc(Tdate *pS) 
{
	pS->Print();
	if(pS->IsLeapYear()) 
	      cout<<"leap year\n";
	else  cout<<"not leap year\n";
}

int main()
{
	Tdate s;
	int m,d,y;
	cout<<"请输入月 日 年:"<<endl;
	cin>>m>>d>>y;
	s.Set(m,d,y);
	someFunc(&s);	
}

*//*

//7-10 使用对象指针完成参数传递 
#include"Tdate_3.h"
#include<iostream>
using namespace std;

void someFunc(Tdate &S) 
{
	S.Print();
	if(S.IsLeapYear()) 
	      cout<<"leap year\n";
	else  cout<<"not leap year\n";
}

int main()
{
	Tdate s;
	int m,d,y;
	cout<<"请输入月 日 年:"<<endl;
	cin>>m>>d>>y;
	s.Set(m,d,y);
	someFunc(s);	
}

*//* 

//7-11 类中有指针且指向堆,默认析构函数不可用,且需复制构造函数 
#include"Student_1.h"
int main()
{
	student s1(1,"wang",18,86);
	s1.printstu();
	student s2(s1);
}

*//* 

//7-12  声明命名空间 
#include"tinyxml.h"
using namespace tinyxml;
int main()
{
    Document doc("text.xml");
    Element elem;
    doc.AddElement(elem);
} 

*/ 

二.头文件

2.1

2.1.1 Clock_1.h

#include<iostream>
using namespace std;
class Tdate
{
  private:
    int month;
    int day;
    int year;
	 	
  public:
    void Set(int,int,int);
    int IsLeapYear();
    void Print();
};

void Tdate::Set(int m, int d, int y)
{
	month = m;
	day = d;
	year = y; 
}

int Tdate::IsLeapYear()
{
	return((year%4 == 0 && year%100 != 0)||(year%400 == 0));
}

void Tdate::Print()
{
	cout<<month<<"/"<<day<<"/"<<year<<endl;		
}

2.1.2 Clock_2.h

#include<iostream>
using namespace std;
class Clock
{
  public:
  	Clock(int H, int M, int S); //¹¹Ô캯Êý
	void SetTime(int newH, int newM, int newS);
	void ShowTime();
  private:
    int Hour;
	int Minute;
	int Second;  
};

Clock::Clock(int H,int M, int S)
{
	Hour = H;
	Minute = M;
	Second = S;
}

void Clock::SetTime(int newH, int newM, int newS)
{
	Hour = newH;
	Minute = newM;
	Second = newS;
} 

void Clock::ShowTime()
{
	cout<<Hour<<":"<<Minute<<":"<<Second<<endl;
}

2.2

2.2.1 Tdate_1

#include<iostream>
using namespace std;
class Tdate
{
  public:
	Tdate();
	Tdate(int d);
	Tdate(int m, int d);
	Tdate(int m, int d, int y);
  //ÆäËû¹«¹²³ÉÔ±
  private:
     int month;
     int day;
     int year;
};

Tdate::Tdate()
{
	month = 4; day = 15; year = 1995;
	cout<<month<<"/"<<day<<"/"<<year<<endl;
}

Tdate::Tdate(int d)
{
	month = 4; day = d; year = 1996;
	cout<<month<<"/"<<day<<"/"<<year<<endl;	
} 

Tdate::Tdate(int m, int d)
{
	month = m; day = d; year = 1997;
	cout<<month<<"/"<<day<<"/"<<year<<endl;	
}

Tdate::Tdate(int m, int d, int y)
{
	month = m; day = d; year = y;
	cout<<month<<"/"<<day<<"/"<<year<<endl;	
}

2.2.2 Tdate_2

#include<iostream>
using namespace std;
class Tdate
{
  public:
	Tdate(int m = 4, int d = 15, int y = 1995)
	{
		month = m; day = d; year = y;
	    cout<<month<<"/"<<day<<"/"<<year<<endl;	
	}
  //ÆäËû¹«¹²³ÉÔ±
  private:
     int month;
     int day;
     int year;
};

2.2.3 Tdate_3

#include<iostream>
using namespace std;
class Tdate
{
  private:
    int month;
    int day;
    int year;
	 	
  public:
    void Set(int,int,int);
    int IsLeapYear();
    void Print();
};

void Tdate::Set(int m, int d, int y)
{
	month = m;
	day = d;
	year = y; 
}

int Tdate::IsLeapYear()
{
	return((year%4 == 0 && year%100 != 0)||(year%400 == 0));
}

void Tdate::Print()
{
	cout<<month<<"/"<<day<<"/"<<year<<endl;		
}

2.3

2.3.1 Student_1.h

#include<iostream>
#include<cstring>
using namespace std;
class student
{
  public:
	student(int, char*, int, float);
	student(student&);
	~student();
	void printstu();
  private:
	int id;
	char* name;
	int age;
	float score;
};

student::student(int i, char* c,int a, float s)
{
	cout<<"Constructing..."<<endl;
	id = i;
	age = a;
	score = s;
	name = new char[strlen(c)+1];
	if(name != 0) strcpy(name,c);
}

student::student(student& s)
{
	cout<<"Copy constructing..."<<endl;
	id = s.id;
	age = s.age;
	score = s.score;
	name = new char[strlen(s.name)+1];
	if(name != 0) strcpy(name,s.name);	
}

student::~student()
{
	cout<<"Destructing..."<<endl;
	delete []name;
	name = 0;
}

void student::printstu()
{
	cout<<"ѧºÅ: "<<id   <<"  "
	    <<"ÐÕÃû: "<<name <<"  "
	    <<"ÄêÁä: "<<age  <<"  "
	    <<"³É¼¨: "<<score<<endl;
}

2.3.2 Student_2.h

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class student
{
  public:
	void set(int, char*, int, float);
	void printstu();
  private:
	int id;
	string name;
	int age;
	float score;
};	

void student::set(int i, char* c, int a, float s)
{
	id = i;
	name = c;
	age = a;
	score = s;
}

void student::printstu()
{
	cout<<"ѧºÅ: "<<id   <<"  "
	    <<"ÐÕÃû: "<<setw(5)<<name <<"  "
	    <<"ÄêÁä: "<<age  <<"  "
	    <<"³É¼¨: "<<score<<endl;	
}

2.3.3 Student_3.h

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class student
{
  public:
	student(int, char*, int, float);
	~student();
	void printstu();
  private:
	int id;
	string name;
	int age;
	float score;
};	

student::student(int i, char* c, int a, float s)
{
	id = i;
	name = c;
	age = a;
	score = s;
	
	cout<<"ѧºÅ"<<id<<" ¹¹Ô캯Êý±»µ÷Óá£"<<endl; 
}

student::~student()
{
	cout<<"ѧºÅ"<<id<<" Îö¹¹º¯Êý "<<endl;
}

void student::printstu()
{
	cout<<"ѧºÅ: "<<id   <<"  "
	    <<"ÐÕÃû: "<<setw(5)<<name <<"  "
	    <<"ÄêÁä: "<<age  <<"  "
	    <<"³É¼¨: "<<score<<endl;	
}

2.3.4 Student_4.h

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class student
{
  public:
	student(int, char*, int, float);
	student(); 
	~student();
	void set(int, char*, int, float);
	void printstu();
  private:
	int id;
	string name;
	int age;
	float score;
};	

student::student(int i, char* c, int a, float s)
{
	id = i;
	name = c;
	age = a;
	score = s;
	
	cout<<"ѧºÅ"<<id<<" ¹¹Ô캯Êý±»µ÷Óá£"<<endl; 
}

student::student()
{
	cout<<"¿Õ¹¹Ôì"<<endl;
}

student::~student()
{
	cout<<"Îö¹¹º¯Êý"<<endl;
}

void student::set(int i, char* c, int a, float s)
{
	id = i;
	name = c;
	age = a;
	score = s;
}

void student::printstu()
{
	cout<<"ѧºÅ: "<<id   <<"  "
	    <<"ÐÕÃû: "<<setw(5)<<name <<"  "
	    <<"ÄêÁä: "<<age  <<"  "
	    <<"³É¼¨: "<<score<<endl;	
}

2.4

2.4.1 tinyxml.h

namespace tinyxml
{
	class Element
	{
	};
	class Document
	{
	  public:
	    Document(char*);
	    int AddElement(const Element&);
	  private:
	    char filename[80];
	};
	tinyxml::Document::Document(char* filename)
	{
	}
	int tinyxml::Document::AddElement(const Element &e)
	{
		return 0;
	} 

} 	

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值