Skip to content

Instantly share code, notes, and snippets.

@jaideepheer
Last active April 5, 2020 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaideepheer/964a3e1ccc054df72e8bdf387f16074a to your computer and use it in GitHub Desktop.
Save jaideepheer/964a3e1ccc054df72e8bdf387f16074a to your computer and use it in GitHub Desktop.
// 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