标签:c++
首先本项目主要是对C++里 面的一些 基础知识和概念进行练习,主要包括:const型函数,函数的返回值,引用,类的组合,以及内存的分配。
下面直接把各个文件的代码写上:
// // Date.h // train2 // // Created by student on 15/8/4. // Copyright (c) 2015年 personals. All rights reserved. // #ifndef train2_Date_h #define train2_Date_h class Date{ public: Date(); Date(int year,int month,int day); void print(); private: int y,m,d; }; #endif
// // Date.cpp // train2 // // Created by student on 15/8/4. // Copyright (c) 2015年 personals. All rights reserved. // #include <iostream> using namespace std; #include "Date.h" Date::Date(){ y=2015; m=1; d=1; } Date::Date(int year,int month,int day){ y=year; m=month; d=day; } void Date::print(){ cout<<"birthday is :"<<y<<"/"<<m<<"/"<<d<<endl; }
// // Person.h // train2 // // Created by student on 15/8/4. // Copyright (c) 2015年 personals. All rights reserved. // #ifndef train2_Person_h #define train2_Person_h #include "Date.h" class Person{ public: Person(); Person(char *Name,char *Email,int day,int month,int year); char * getName(); char * getEmail_Address(); Date& getBirthday(); void print(); private: char *name; char *email; Date date; }; #endif
// // Person.cpp // train2 // // Created by student on 15/8/4. // Copyright (c) 2015年 personals. All rights reserved. // #include "Person.h" #include <iostream> #include <stdlib.h> using namespace std; #include "string.h" Person::Person():date(2015,1,1){ char n1[]="hh"; char n2[]="hh@163.com"; name =n1 ; email =n2; } Person::Person(char *Name,char *Email,int day,int month,int year):date(year,month,day){ name = (char *)malloc(sizeof(char)*20); email = (char *)malloc(sizeof(char)*20); strcpy(name,Name); strcpy(email,Email); } char * Person::getName(){ return name; } char * Person::getEmail_Address(){ return email; } void Person::print(){ cout<<"name="<<name<<",email="<<email<<endl; date.print(); } Date& Person::getBirthday(){ return this->date; }
//PersonSet是Person的容器,即Person的集合; // PersonSet.h // train2 // // Created by student on 15/8/4. // Copyright (c) 2015年 personals. All rights reserved. // #ifndef train2_PersonSet_h #define train2_PersonSet_h #include "Person.h" class PersonSet{ public: PersonSet(int capacity=9,int initial_size=0); ~PersonSet(); public: void Add(Person & element);//增加peson成员; Person & NextElement(); //迭代下一个对象; Person & RemoveElement(); //删除最后一个Person对象,并返回; Person & RemoveElement(int index); //删除第index个对象,并返回; void reset(); //将容器重置大小为0; int size() const; //返回容器里的Person对象的数量; void print() const; private: Person ** _elements; int _capacity; //容量 int _size; //对象个数 int _index; //索引值 //static int count; }; //int PersonSet::count=0; #endif
// // PersonSet.cpp // train2 // // Created by student on 15/8/4. // Copyright (c) 2015年 personals. All rights reserved. // #include <iostream> using namespace std; #include "PersonSet.h" PersonSet::PersonSet(int capacity,int initial_size){ _capacity = capacity; _size = initial_size; _elements = new Person * [_capacity]; //count++;_size = count; } int PersonSet::size() const{ return _size; } //增容原理:1.先定义一个对象的二级指针指向原对象的二级指针 //2.根据需要给原对象重新开辟空间 //3.通过第一步定义的指针给新开辟的对象指针复制 //4.最后删除定义的指针。 void PersonSet::Add( Person& aPerson ) { // Relocate the array space if ( _size == _capacity ) { Person** temp = _elements; _elements = new Person*[_capacity*2]; for( int i=0; i<_size; i++ ) { _elements [i] = temp[i]; } _capacity*= 2; delete temp; } _elements[_size++] = &aPerson; } Person& PersonSet:: RemoveElement() { _size--; Person* p = new Person; p=_elements[_size]; //shrink the container if(_size < _capacity/2) { cout << "shrinking ! Capacity is "<<_capacity<<endl; Person** temp = _elements; _elements= new Person*[_capacity/2]; for ( int i=0; i<_size; i++ ) { _elements [i]= temp[i]; } _capacity /= 2; delete temp; } return *p; } Person& PersonSet::NextElement() { Person* p = new Person; if (_elements[_size]!=NULL) { p = _elements[_size]; _size++; } else { delete p; p=NULL; } return *p; } void PersonSet::reset(){ _size=0; } void PersonSet::print() const{ int i=0; for (; i<_size; i++) { cout<<"name:"<<_elements[i]->getName()<<endl; } } Person& PersonSet::RemoveElement(int index){ Person* p = new Person; if (index>_size || index<=0) { cout<<"index is out of side!\n"; p = NULL; return *p; } int x; Person *p1 = _elements[--index]; for(x=index;x<_size-1;x++){ _elements[x]=_elements[x+1]; } int i = _size-1; _elements[i] = p1; *p = RemoveElement(); return *p; } PersonSet::~PersonSet(){ }
// // main.cpp // train2 // // Created by student on 15/8/4. // Copyright (c) 2015年 personals. All rights reserved. // #include <iostream> #include <iostream> #include <string.h> #include "Date.h" #include "Person.h" #include "PersonSet.h" using namespace std; int main(int argc, const char * argv[]) { //declare some const persons Person *p1 = new Person("Lou", "lou@chat.ca", 20, 6, 1960); Person *p2 = new Person("Frank", "f123@chat.ca", 20, 3, 1967); Person *p3 = new Person("Ann", "ann@chat.ca", 20, 8, 1960); PersonSet boys, girls; boys.Add(*p1); //test to see if the same object is retrieved from the set. if (p1 != &boys.RemoveElement() ) { cout << "ERROR: the objects are different \n"; } else { cout << "Good, the objects are the same \n"; } boys.Add( *p1); boys.Add( *p2); girls.Add( *p3); boys.Add(*(new Person("John", "f123@chat.ca", 20, 3, 1967))); girls.Add(*(new Person("Sue", "f123@chat.ca", 20, 3, 1967))); boys.Add(*(new Person("Frank", "frank@chat.ca", 25, 4, 1958))); girls.Add(*(new Person("Mary", "mary@chat.ca", 25, 4, 1955))); boys.Add(*(new Person("John", "johnchat.ca", 12, 12, 1970))); //print all the boys using the removeSomeElement() method and delete //them int numberOfBoys = boys.size(); cout << "number of boys = " << numberOfBoys << "\n"; for(int i = 0; i<numberOfBoys; i++) { Person & boy = boys.RemoveElement(); if (&boy == NULL) { break; } boy.print(); delete &boy; } cout << "number of girls = " << girls.size() << "\n"; girls.print(); //print of the girls birthdays and using the someElement() method*/ int numberOfGirls =girls.size(); girls.reset(); for(int i = 0; i<numberOfGirls; i++) girls.NextElement().getBirthday().print(); //delete all the girls from the heap Person& hh=girls.RemoveElement(3); hh.print(); delete &hh; numberOfGirls = girls.size(); for(int i = 0; i<numberOfGirls; i++) { Person & her = girls.RemoveElement(); her.print(); delete &her; } return 0; }
本文出自 “c0c0s” 博客,请务必保留此出处http://9362125.blog.51cto.com/9352125/1683109
标签:c++
原文地址:http://9362125.blog.51cto.com/9352125/1683109