Question

Cout_op.hpp #include <ostream> #include <string> #pragma once #ifndef STUDENT_CPP #define STUDENT_CPP #includes "Student.cpp" #endif //This allows...

Cout_op.hpp

#include <ostream>
#include <string>
#pragma once
#ifndef STUDENT_CPP
#define STUDENT_CPP
#includes "Student.cpp"
#endif

//This allows us to << a Student object
template<class CharT, class TraitsT>
std::basic_ostream<CharT, TraitsT>&
operator <<(std::basic_ostream<CharT, TraitsT>& os, Student& sc)
{
   return os << sc.toString();
}
class Student
{
private:
//declaring private variables
struct Student () {
string name;
int age;
char gender;
double gpa;
bool onScholarship};
public:
//creating two constructors prototype
Student(); //empty constructor
Student(string,int,char,double,bool);//parameterised constructor

//declaring prototype of 6 methods
int getAge();
void setGPA(double);
void setOnScholarship(bool);
string toString();
bool onProbation();
string complain();
};
Student.cpp

/* File: Student.cpp
* Name: ************
* Date: MM/DD/YYYY
* Purpose: Show your understanding of classes
*/
#include <iostream>
#include <string>

using namespace std;

class Student
//empty constructor
Student::Student(){

}
//parameterised constructor
Student::Student(string name,int age,char gender,double gpa,bool onScholarship){
Student::Student name=name;
Student::Student age=age;
Student::Student gender=gender;
Student::Student gpa=gpa;
Student::Student onScholarship=onScholarship;

}
//returning age
int Student::getAge(){
return age;
}
//setting GPA
void Student::setGPA(double gpa){
this->gpa=gpa;
}
//Setting onScholarship
void Student::setOnScholarship(bool onScholarship){
this->onScholarship=onScholarship;
}
//creating fomatted string
string Student::toString(){
return (name + "\t" + to_string(age) +"\t" +gender+"\t"+std::to_string(gpa)+"\t"+std::to_string(onScholarship));
}
//method to check whether is probation
bool Student::onProbation(){
if(onScholarship && gpa<2)
return true;
else
return false;
}
//Method to return complain
string Student::complain(){
return "I have complain about toilets";
}

StudentDriver.cpp

#include <iostream>
#include <ostream>
#include <string>
#includes Student.cpp
#include "Cout_op.hpp" //includes Student.cpp

using namespace std;

// This will be the "driver" class for the Student class created in
// MinilabWritingClasses (It looks very complicated because of all
// the comments, but it really just creates instances of Student and
// tells them to do things...)

int main()
{
   //create an instance of Student
   cout << "***** Creating a Student, uninitialized data from default constructor\n";
   Student stud1; //calls default constructor

   //print it so we can see what the default values were for the class data
   //note that its toString() will be called automatically
   cout << "\n***** printing it - notice the uninitialized values\n";
   cout << stud1 << endl;

   //create another instance of a Student, passing in initial values to its constructor
   cout << "\n***** Creating another Student, passing initial values to its constructor\n";
   Student msBoss = Student("Bill Gates", 57, 'm', 3.2, true);

   //tell it to return its age
   cout << "\n***** telling it to return its age.\n";
   int theAge = msBoss.getAge();
   cout << "Its age is: " << theAge << endl;

   //print it - note that its toString() will be called automatically;
   cout << "\n***** printing it - see if values are correct\n";
   cout << msBoss << endl;

   //ask it if it is on probation
   cout << "\n***** asking it if it is on probation (check answer)\n";
   cout << "onProbation() returned: " << msBoss.onProbation() << endl;

   //tell it to change its gpa to 1.3
   cout << "\n***** telling it to change its gpa to 1.3\n";
   msBoss.setGPA(1.3);

   //print it now
   cout << "\n***** printing it - see if the values are correct\n";
   cout << msBoss << endl;

   //ask it if it is on probation now
   cout << "\n***** asking it if it is on probation (check answer)\n";
   cout << "onProbation() returned: " << msBoss.onProbation() << endl;

   //tell it to complain
   cout << "\n***** telling it to complain\n";
   cout << "complain() returned: " << msBoss.complain() << endl;

   //tell it to change its onScholarship field to false
   cout << "\n***** telling it to change its onScholarship field to false\n";
   msBoss.setOnScholarship(false);

   //print it now
   cout << "\n***** printing it - see if the values are correct\n";
   cout << msBoss << endl;

   //ask it if it is on probation now
   cout << "\n***** asking it if it is on probation (check answer)\n";
   cout << "onProbation() returned: " << msBoss.onProbation() << endl;

   //create a different student, tell it to have some different values, and tell it to print itself
   cout << "\n***** creating a different Student, passing initial values to its constructor/n";
   Student stud2;
   stud2 = Student("Hillary Clinton", 59, 'f', 2.0, true);        //notice-can define variable and create it in 2 steps

   //print it
   cout << "\n***** printing it - see if the values are correct\n";
   cout << stud2 << endl;

   //ask it if it is on probation now
   cout << "\n***** asking it if it is on probation (check answer)\n";
   cout << "onProbation() returned: " << stud2.onProbation() << endl;

   cin.get(); cin.get();
   return 0;

}//end main()

I am having an issue running this code on CodeBlocker for c++ class. can you please tell me what the problem is? thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

There was a few problems with your code:-

1. There was problem with your structure declaration in student.cpp code

2. The student class created by you should be in student.cpp class not in cout_op.hpp

3. changing all the variables with correspong ding correct structure declaration.

I have corrected the codes and these work fine as tested on code blocks screenshot attached:

**********************

Cout_op.hpp

*************************

#include <ostream>
#include <string>
#pragma once
#ifndef STUDENT_CPP
#define STUDENT_CPP
#include "Student.cpp"
#endif
//This allows us to << a Student object
template<class CharT, class TraitsT>
std::basic_ostream<CharT, TraitsT>&
operator <<(std::basic_ostream<CharT, TraitsT>& os, Student& sc)
{
return os << sc.toString();
}

************************************************

Student.cpp

***********************************************

/* File: Student.cpp
* Name: ************
* Date: MM/DD/YYYY
* Purpose: Show your understanding of classes
*/
#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
//declaring private variables
/* changed structure declaration */

struct Student_data {
string name;
int age;
char gender;
double gpa;
bool onScholarship;
}s;
/***********************/
public:
//creating two constructors prototype
Student(); //empty constructor
Student(string,int,char,double,bool);//parameterised constructor
//declaring prototype of 6 methods
int getAge();
void setGPA(double);
void setOnScholarship(bool);
string toString();
bool onProbation();
string complain();
};
//empty constructor
Student::Student(){
}
//parameterised constructor
Student::Student(string name,int age,char gender,double gpa,bool onScholarship){
Student::s.name=name;
Student::s.age=age;
Student::s.gender=gender;
Student::s.gpa=gpa;
Student::s.onScholarship=onScholarship;
}
//returning age
int Student::getAge(){
return s.age;
}
//setting GPA
void Student::setGPA(double gpa){
this->s.gpa=gpa;
}
//Setting onScholarship
void Student::setOnScholarship(bool onScholarship){
this->s.onScholarship=onScholarship;
}
//creating fomatted string
string Student::toString(){
return (s.name + "\t" + to_string(s.age) +"\t" +s.gender+"\t"+std::to_string(s.gpa)+"\t"+std::to_string(s.onScholarship));
}
//method to check whether is probation
bool Student::onProbation(){
if(s.onScholarship && s.gpa<2)
return true;
else
return false;
}
//Method to return complain
string Student::complain(){
return "I have complain about toilets";
}


**************************************************************

StudentDriver.cpp

**************************************************************


#include <iostream>
#include <ostream>
#include <string>
//#includes Student.cpp
#include "Cout_op.hpp" //includes Student.cpp
using namespace std;
// This will be the "driver" class for the Student class created in
// MinilabWritingClasses (It looks very complicated because of all
// the comments, but it really just creates instances of Student and
// tells them to do things...)
int main()
{
//create an instance of Student
cout << "***** Creating a Student, uninitialized data from default constructor\n";
Student stud1; //calls default constructor
//print it so we can see what the default values were for the class data
//note that its toString() will be called automatically
cout << "\n***** printing it - notice the uninitialized values\n";
cout << stud1 << endl;
//create another instance of a Student, passing in initial values to its constructor
cout << "\n***** Creating another Student, passing initial values to its constructor\n";
Student msBoss = Student("Bill Gates", 57, 'm', 3.2, true);
//tell it to return its age
cout << "\n***** telling it to return its age.\n";
int theAge = msBoss.getAge();
cout << "Its age is: " << theAge << endl;
//print it - note that its toString() will be called automatically;
cout << "\n***** printing it - see if values are correct\n";
cout << msBoss << endl;
//ask it if it is on probation
cout << "\n***** asking it if it is on probation (check answer)\n";
cout << "onProbation() returned: " << msBoss.onProbation() << endl;
//tell it to change its gpa to 1.3
cout << "\n***** telling it to change its gpa to 1.3\n";
msBoss.setGPA(1.3);
//print it now
cout << "\n***** printing it - see if the values are correct\n";
cout << msBoss << endl;
//ask it if it is on probation now
cout << "\n***** asking it if it is on probation (check answer)\n";
cout << "onProbation() returned: " << msBoss.onProbation() << endl;
//tell it to complain
cout << "\n***** telling it to complain\n";
cout << "complain() returned: " << msBoss.complain() << endl;
//tell it to change its onScholarship field to false
cout << "\n***** telling it to change its onScholarship field to false\n";
msBoss.setOnScholarship(false);
//print it now
cout << "\n***** printing it - see if the values are correct\n";
cout << msBoss << endl;
//ask it if it is on probation now
cout << "\n***** asking it if it is on probation (check answer)\n";
cout << "onProbation() returned: " << msBoss.onProbation() << endl;
//create a different student, tell it to have some different values, and tell it to print itself
cout << "\n***** creating a different Student, passing initial values to its constructor/n";
Student stud2;
stud2 = Student("Hillary Clinton", 59, 'f', 2.0, true); //notice-can define variable and create it in 2 steps
//print it
cout << "\n***** printing it - see if the values are correct\n";
cout << stud2 << endl;
//ask it if it is on probation now
cout << "\n***** asking it if it is on probation (check answer)\n";
cout << "onProbation() returned: " << stud2.onProbation() << endl;
cin.get(); cin.get();
return 0;
}//end main()

**********************************************************************

Add a comment
Know the answer?
Add Answer to:
Cout_op.hpp #include <ostream> #include <string> #pragma once #ifndef STUDENT_CPP #define STUDENT_CPP #includes "Student.cpp" #endif //This allows...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string;...

    Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string; class Account { public: Account(); Account(string, double); void deposit(double); bool withdraw(double); string getName() const; double getBalance() const; private: string name; double balance; }; #endif ////////////////////////////////////////////// //AccountManager.hpp #ifndef _ACCOUNT_MANAGER_HPP_ #define _ACCOUNT_MANAGER_HPP_ #include "Account.hpp" #include <string> using std::string; class AccountManager { public: AccountManager(); AccountManager(const AccountManager&); //copy constructor void open(string); void close(string); void depositByName(string,double); bool withdrawByName(string,double); double getBalanceByName(string); Account getAccountByName(string); void openSuperVipAccount(Account&); void closeSuperVipAccount(); bool getBalanceOfSuperVipAccount(double&) const;...

  • In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double...

    In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double length; double height; double tailLength; string eyeColour; string furClassification; //long, medium, short, none string furColours[5]; }; void initCat (Cat&, double, double, double, string, string, const string[]); void readCat (Cat&); void printCat (const Cat&); bool isCalico (const Cat&); bool isTaller (const Cat&, const Cat&); #endif ***//Cat.cpp//*** #include "Cat.h" #include <iostream> using namespace std; void initCat (Cat& cat, double l, double h, double tL, string eC,...

  • lex.h ----------------- #ifndef LEX_H_ #define LEX_H_ #include <string> #include <iostream> using std::string; using std::istream; using std::ostream;...

    lex.h ----------------- #ifndef LEX_H_ #define LEX_H_ #include <string> #include <iostream> using std::string; using std::istream; using std::ostream; enum Token { // keywords PRINT, BEGIN, END, IF, THEN, // an identifier IDENT, // an integer and string constant ICONST, RCONST, SCONST, // the operators, parens, semicolon PLUS, MINUS, MULT, DIV, EQ, LPAREN, RPAREN, SCOMA, COMA, // any error returns this token ERR, // when completed (EOF), return this token DONE }; class LexItem { Token token; string lexeme; int lnum; public: LexItem()...

  • Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include #include using namespace std;...

    Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include #include using namespace std; class Server { public:    Server();    Server(string, int);    ~Server();    string getPiece(int); private:    string *ascii;    mutex access; }; #endif -------------------------------------------------------------------------------------------------------------------------- Server.cpp #include "Server.h" #include #include Server::Server(){} Server::Server(string filename, int threads) {    vector loaded;    ascii = new string[threads];    ifstream in;    string line;    in.open(filename);    if (!in.is_open())    {        cout << "Could not open file...

  • //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;   ...

    //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;    double mpg;    Warranty warranty;    static int numOfVehicles; public:    Vehicle();    Vehicle(string s, int y, double m, Warranty warranty);    Vehicle(Vehicle& v);    ~Vehicle();    string getMake();    int getYear();    double getGasMileage();    void setMake(string s);    void setYear(int y);    void setYear(string y);    void setGasMileage(double m);    void setGasMileage(string m);    void displayVehicle();    static int getNumVehicles();    Warranty getWarranty();    void setWarranty(Warranty& ); }; //Vehicle.cpp #include "Vehicle.h" #include <string> Vehicle::Vehicle() {    make = "unknown";    year =...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • //stack_exception.h #ifndef STACK_EXCEPTION #define STACK_EXCEPTION #include <string> using namespace std; class Stack_Exception { public: Stack_Exception(string what)...

    //stack_exception.h #ifndef STACK_EXCEPTION #define STACK_EXCEPTION #include <string> using namespace std; class Stack_Exception { public: Stack_Exception(string what) : what(what) {} string getWhat() {return what;} private: string what; }; #endif //stack_test_app.cpp #include <iostream> #include <sstream> #include <string> #include "stack.h" using namespace std; /********************************************* * The 'contains' function template goes here * *********************************************/ int main() {    cout << boolalpha;    cout << "--- stack of int" << endl;    Stack<int> si;    cout << "si intially " << si << endl;   ...

  • CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare...

    CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node {    char c;    Node *next; }; class LinkedString {    Node *head; public:    LinkedString();    LinkedString(char[]);    LinkedString(string);    char charAt(int) const;    string concat(const LinkedString &obj) const;    bool isEmpty() const;    int length() const;    LinkedString substring(int, int) const;    //added helper function to add to linekd list private:    void add(char c); };...

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT