



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




? :
passing by value passing by reference with pointer argument passing by reference with reference argument
const Sentence Sentence:: operator ()(int begin, int end) { Sentence s; for (int i=begin; i <= end; i++) s+= word[i]; return s; }
If an overloaded operator function is a member of class, by default, the left operand is the object of the class. But for the operators << and >>, the left operand is a reference to an object of class istream or ostream.
1 // --file "Student.h" begins here -- 2 #ifndef STUDENT_H 3 #define STUDENT_H 4 5 #include
8 using namespace std; 9 10 class Student 11 { 12 public: 13 Student(){ cout << "Inside Student Constructor"<< endl; } 14 Student(char *, int); 15 16 const char * GetName() const; 17 void SetName(char *); 18 int GetId() const; 19 void SetId(int); 20 21 void print() const; 22 23 private: 24 char * name; 25 int studentid; 26 }; 27 #endif 28 29 // --file Student.cpp" begins here 30 #include "student.h" 31 32 Student:: Student(char * n, int id) 33 { 34 name = new char [strlen(n) +1]; 35 name = n; 36 studentid = id; 37 } 38 39 const char *Student:: GetName() const 40 { 41 return name; 42 } 43 44 void Student:: SetName(char * n) 45 { 46 name = new char [strlen(n) +1]; 47 name = n; 48 } 49 50 int Student:: GetId() const 51 { 52 return studentid; 53 } 54 55 void Student::SetId(int id) 56 { 57 studentid= id; 58 } 59 60 void Student :: print() const
117 Student s1("Peter Gallager", 123456789); 118 s1.print(); 119 s1.SetName("Robert Horry"); 120 s1.print(); 121 122 UndergraduateStudent ug1("John Smith", 615182223, ’U’); 123 ug1.print(); 124 cout << ug1.GetId() << endl; 125 126 return 0; 127 }
Because these functions do not modify class members.
To hide the actual implementation; a notion popularly known as information hiding.
Reduces function call overhead.
Reduces coding overhead and eases management for the derived class by reusing member definitions of the base class.
To allocate memory for character array dynamically.
To initialize base class data members.
Student Name:Peter Gallager Student id: Student Name:Robert Horry Student id: Student Name:John Smith Student id: Code: U 615182223
UndergraduateStudent :: UndergraduateStudent(char *n, int id, char cd) { SetName(n); SetId(id); SetCode(cd); }
Student Name:Peter Gallager Student id: Student Name:Robert Horry Student id: Inside Student Constructor Student Name:John Smith Student id: Code: U 615182223
student: Student.o UndergraduateStudent.o main.o g++ -o student Student.o UndergraduateStudent.o main.o
Student.o: Student.cpp Student.h g++ -c Student.cpp
UndergraduateStudent.o: UndergraduateStudent.cpp UndergraduateStudent.h g++ -c UndergraduateStudent.cpp
main.o: main.cpp UndergraduateStudent.h Student.h g++ -c main.cpp
clean: rm Student.o UndergraduateStudent.o main.o student