{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lec11MiscClassTopics_6per.pdf

Lec11MiscClassTopics_6per.pdf - class StudentClass void...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 The University Of Michigan Andrew M. Morgan EECS402 Lecture 11 Savitch Ch 6 Misc. Class/Object Topics EECS 402 EECS 402 Andrew M Morgan 2 Compiler Error Example class StudentClass { public: string name; int id; public: StudentClass(const string &inName, int inId); }; //Global function prototypes are here int main() { StudentClass s1; s1.name = "Robert"; s1.id = 4827; printStudentInfo(s1); return (0); } StudentClass::StudentClass( const string &inName, int inId ) { name = inName; id = inId; } void printStudentName( StudentClass &student ) { cout << student.name; } void printStudentInfo( const StudentClass &student ) { cout << "Name: "; printStudentName(student); cout << " Id: " << student.id << endl; } c.cpp: In function 'int main()': c.cpp:56: no matching function for call to 'StudentClass::StudentClass()' Recall: Once a ctor is provided, the default ctor is no longer available (unless a default ctor is provided as an overloaded ctor) EECS 402 EECS 402 Andrew M Morgan 3 Fix One Error, Add Another c.cpp: In function `void printStudentInfo(const StudentClass &)': c.cpp:50: conversion from `const StudentClass' to `StudentClass &' discards qualifiers c.cpp:41: in passing argument 1 of `printStudentName(StudentClass &)' Note: printStudentInfo() has "promised" that the student object passed in will not be changed, via the keyword const in the parameter list. The object is then passed into the printStudentName() function, which does NOT make the same promise. Even though the object isn't changed, the compiler sees that it CAN change, and provides the compiler error shown. class StudentClass { public: string name; int id; public: StudentClass(const string &inName, int inId); }; //Global function prototypes are here int main() { StudentClass s1 ("Robert", 4827) ; printStudentInfo(s1); return (0); } StudentClass::StudentClass( const string &inName, int inId ) { name = inName; id = inId; } void printStudentName( StudentClass &student ) { cout << student.name; } void printStudentInfo( const StudentClass &student ) { cout << "Name: "; printStudentName(student); cout << " Id: " << student.id << endl; } EECS 402 EECS 402 Andrew M Morgan 4 Updated With No Compiler Errors class StudentClass { public: string name; int id; public: StudentClass(const string &inName, int inId); }; //Global function prototypes are here int main() { StudentClass s1("Robert", 4827); printStudentInfo(s1); return (0); } StudentClass::StudentClass( const string &inName, int inId ) { name = inName; id = inId; } void printStudentName( const StudentClass &student ) { cout << student.name; } void printStudentInfo( const StudentClass &student ) { cout << "Name: "; printStudentName(student); cout << " Id: " << student.id << endl; } Name: Robert Id: 4827 EECS 402 EECS 402 Andrew M Morgan 5 New Version Using Member Function, Error class StudentClass { public: string name; int id; public: StudentClass(const string &inName, int inId); string getName(); int getId(); }; //Global function prototypes are here int main() { StudentClass s1("Robert", 4827); printStudentInfo(s1); return (0); } StudentClass::StudentClass( const string &inName, int inId ) { name = inName; id = inId; } int StudentClass::getId( ) { return (id); } string StudentClass::getName( ) { return (name); } void printStudentInfo( const StudentClass &student ) { cout << "Name: "; cout << student.getName(); cout << " Id: " << student.id << endl;
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern