当前位置: 首页>>代码示例>>C++>>正文


C++ Student::GetFirstName方法代码示例

本文整理汇总了C++中Student::GetFirstName方法的典型用法代码示例。如果您正苦于以下问题:C++ Student::GetFirstName方法的具体用法?C++ Student::GetFirstName怎么用?C++ Student::GetFirstName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Student的用法示例。


在下文中一共展示了Student::GetFirstName方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1:

/*
	Name: == (overload)
	Function:
		Checks if Student is equal to Student s
	Parameters:
		s (Student)			Other Student being compared
	Returns: (bool) true if Student is equal to Student s
*/
bool Student::operator==(Student s) {
	return (mFirstName == s.GetFirstName() && mLastName == s.GetLastName());
}
开发者ID:Dgdrgn,项目名称:StudentRandomizer,代码行数:11,代码来源:Student.cpp

示例2: main

int main(int argc, char* argv[]) {
   // I can declare three types of variables: UniversityMember, Student, Faculty
   Student bob("Bob", "Roberts", 12345, "Computer Science", 2011, 2015);
   
   // We can call any public methods declared in Student or UniversityMember 
   bob.SetFirstName("Bobby");
   cout << (string)bob << endl;

   Faculty neal("Neal", "Terrell", 99999, "Computer Engineering and Computer "
    "Science", "VEC-404A");
   cout << (string)neal << endl;

   // I can also declare a variable of type UniversityMember
   UniversityMember dean("Forouzan", "Golshani", 1);
   // why can't I do the next line?
   // cout << (string)dean << endl;

   
   // How do pointers work with this?
   Student *pStudent = &bob; // a Student pointer to bob
   Faculty *pFaculty = &neal; // a Faculty pointer to neal
   UniversityMember *pUni = &dean;

   // I can do what you would expect with these
   cout << pStudent->GetFirstName() << " started in year " << 
    pStudent->GetStartYear() << endl;
     
   // can I assign from one type to another?
   // neal = bob; // error: no operator= matches these arguments

   // so no, I can't assign from one type to another... but can I take pointers
   // from one type to another??
   // pStudent = &neal;   error: Faculty* cannot be assigned to Student*

   // but I CAN do this!
   pUni = &neal;

   // because Faculty "is-a" UniversityMember, I can assign a Faculty* to a
   // UniversityMember*. 
   // what methods can I call on pUni now?
   cout << pUni->GetFirstName();
   



   // so even though pUni points to an actual Faculty object, I can only call
   // functions from the UniversityMember class on that pointer. WHY CAN'T I 
   // CALL FACULTY FUNCTIONS ON THIS POINTER???




   // to summarise: a pointer to a base type can actually point to any derived
   // type object. You can then call any functions defined at the base class
   // on that pointer.

   
   pUni = &bob;
   // How about taking a base type pointer and using it as a derived type 
   // pointer? This is called "down-casting"
   
   // Can we just assign the base pointer to a derived pointer?
   // pStudent = pUni; //cannot assign UniversityMember* to Student*

   // could pUni *potentially* point to a Student?
   // not the same question as "*does* it point to a Student"

   // yes, it can potentially point to a Student, so we tell the compiler we 
   // know what we're doing by using a cast:
   pStudent = (Student *)pUni; 
   cout << endl << "Start year for pStudent: " << pStudent->GetStartYear() 
    << endl;

   // down-casting can be dangerous if you get the types wrong.
   pUni = &neal; // what does pUni point to now? is that a Student?
   pStudent = (Student *)pUni; // can the compiler detect this is wrong?
   cout << endl << "Start year for new pStudent: " << pStudent->GetStartYear() 
    << endl;



   // one final demo:
   vector<UniversityMember *> members; // a vector of UMember pointers
   members.push_back(&bob); // I can push a pointer to a Student; why?
   members.push_back(&neal); // I can push a pointer to a Faculty; why?
   members.push_back(&dean); // I can push a pointer to a UniversityMember; why?

   cout << endl << endl << "All UniversityMembers created so far:" << endl;
   typedef vector<UniversityMember *>::iterator umItr;
   for (umItr itr = members.begin(); itr != members.end(); itr++) {
      // *itr gives me a UniversityMember*
      cout << (*itr)->GetFirstName() << " " << (*itr)->GetLastName() << endl;
   }
}
开发者ID:csulb-cecs282-2015sp,项目名称:lectures,代码行数:94,代码来源:main.cpp


注:本文中的Student::GetFirstName方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。