Last active
April 5, 2020 13:25
-
-
Save jaideepheer/964a3e1ccc054df72e8bdf387f16074a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example program | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
class Person | |
{ | |
public: | |
Person(){} | |
}; | |
class Student : public Person | |
{ | |
public: | |
int *scores; | |
int count; | |
string fname, lname; | |
Student(string fname, string lname, int scores[], int noOfSubjects) | |
{ | |
this->fname = fname; | |
this->lname = lname; | |
this->scores = scores; | |
this->count = noOfSubjects; | |
} | |
float getAverageScore() | |
{ | |
int sum = 0; | |
for(int i=0; i< count; ++i) | |
{ | |
sum+=scores[i]; | |
} | |
return (float)sum/count; | |
} | |
}; | |
// to convert int array to string for cout | |
#include<algorithm> | |
std::ostream& printArray(std::ostream &strm, int* &a, int size) | |
{ | |
strm << "["; | |
for(int i=0; i<size; ++i) | |
{ | |
strm << " " << a[i] << ","; | |
} | |
return strm << "]"; | |
} | |
// to convert Student object to string for cout | |
std::ostream& operator<<(std::ostream &strm, Student &a) | |
{ | |
strm | |
<< "Student( " | |
<< '"' << a.fname << " " << a.lname << '"' | |
<< ", " | |
<< "noOfMarks: " << a.count | |
<< ", " | |
<< "averageScore: " << a.getAverageScore() | |
<< ", " | |
<< "scores: "; | |
return printArray(strm, a.scores, a.count) << " )"; | |
} | |
int main() | |
{ | |
int marks[] = {100,100,100,100,100,50}; | |
Student s("eilu","rondu", marks, 6); | |
cout << s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment