c++ - How to run below codes inside the Student.cpp through a class? -
// main.cpp #include <iostream> #include <vector> #include <string> #include "student.h" using namespace std; void fillvector(vector<student>&); void printvector(const vector<student>&); int main() { vector<student> myclass; fillvector(myclass); printvector(myclass); return 0; } void fillvector(vector<student>& newmyclass) { string name; char grade; cout << "how many students in class? "; int classsize; cin >> classsize; (int i=0; i<classsize; i++) { cout<<"enter student name: "; cin>>name; cout<<"enter student grade: "; cin>>grade; student newstudent(name, grade); newmyclass.push_back(newstudent); cout<<endl; } cout<<endl; } void printvector(const vector<student>& newmyclass) { int size = newmyclass.size(); ( int i=0; i<size; i++ ) { cout<<"student name: "<<newmyclass[i].getname()<<endl; cout<<"student grade: "<<newmyclass[i].getgrade()<<endl; cout<<endl; } } // student.h #ifndef student_h_included #define student_h_included #include <iostream> #include <string> using namespace std; class student { public: student(); student(string, char); ~student(); string getname() const; char getgrade() const; void setname(string); void setgrade(char); private: string newname; char newgrade; }; #endif // student_h_included // student.cpp #include "student.h" student::student() { newgrade = ' '; } student::student(string name, char grade) { newname = name; newgrade = grade; } student::~student() {} string student::getname() const { return newname; } char student::getgrade() const { return newgrade; } void student::setname(string name) { newname = name; } void student::setgrade(char grade) { newgrade = grade; }
my aim declare class called creategradebook inside student.h
and define in student.cpp
, put code of main.cpp
in it. in other words want main.cpp
still there no codes in below;
#include <iostream> using namespace std; int main() { }
please tolerant if question inappropriate or off topic i'm new stackoverflow. i've read faq section not of it.
well here's start
class creategradebook { public: void fillvector(); void printvector() const; private: vector<student> myclass; };
notice how vector declared in main has become private data member myclass
. 2 functions declared have become public methods of creategradebook
. notice have lost parameters instead they'll operate on private data member myclass
.
i'll leave rest.
Comments
Post a Comment