| 0 | kirasiris | Categories: Tags: , , , , , ,

struct

A very basic introduction to the differences between using struct and class objects. As mentioned before you can access and play with this program on repl where I have been publishing most of the programs that I have had created in my C++ class.

// The Struct vs. the Class
// Student Kevin
#include <iostream>
#include <string>
using namespace std; 

struct Student
{
   int id; 
   string name;
   double credits; 
};  

class Graduate
{
  private:
    int id;
    string name; 
    double credits;
  public:
    void setID(int);   
    int getID() const; 
    void setName(string);   
    string getName() const; 
    void setCredits(double);
    double getCredits() const; 
    double calcGPA() const;
};

void Graduate::setID(int i)
{
     id = i;  //set the private attribute member, id to i
}

int Graduate::getID() const
{
      return id; //return the private member variable, id
}

void Graduate::setName(string n)
{
      name = n; //set the private attribute member, name to n 
}

string Graduate::getName() const 
{
	return name; //return the private member variable, name 
}

void Graduate::setCredits(double c)
{
  credits = c; 
}

double Graduate::getCredits() const
{
  return credits; 
}

double Graduate::calcGPA() const
{
  return (credits < 4.8);
}

int main()
{
   Student s; 
    s.id = 12;
    s.name = "Tyson";
    s.credits = 12;

    cout << " Id: " << s.id << " name: " << s.name << endl;

    // Instatiate Graduate g;
    Graduate g;
      g.setID(13);
      g.setName("Kevin");
      g.setCredits(130);
      g.calcGPA();

    cout << " Id: " << g.getID() <<  " Name: " << g.getName() << endl;
    cout << " Credits: " << g.getCredits() << " GPA: " << g.calcGPA() << endl;


   return 0;
}

If you found the struct object to be interesting, tell me how do you use them?!. That is it for today. You can find more articles related to C++ at: C++ Snippets!.

Please leave a comment below in you would like me to post anything else than just snippets. I’m very aware that I’m not at good explaining but if you need help in your assignments, don’t be afraid to reach out to me!.

Leave a Reply

Back to Top