Question

C++ program Create a Student class that contains three private data members of stududentID (int), lastName...

C++ program

Create a Student class that contains three private data members of stududentID (int), lastName (string), firstName(string) and a static data member studentCount(int).

  • Create a constructor that will take three parameters of studentID, lastName and firstName, and assign them to the private data member. Then, increase the studentCount in the constructor.
  • Create a static function getStudentCount() that returns the value of studentCount.
  • studentCount is used to track the number of student object has been instantiated. Initialize it to 0 outside of the class implementation.

Write a main function:

  • print out the value of studentCount by using the scope resolution operator Student:: and getStuentCount()
  • instantiate two students S1 and S1 by making up the studentIDs, firstNames and lastNames.
  • print outthe value of studentCount again.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;
//class student
class student
{
    private:
        //data members
        int studentID;
        string lastName,firstName;
        static int studentCount;
    public: //member functions
      student(int,string,string)   ;
      static int getStudentCount();
};
//parameterized constructor
student :: student(int a,string fname,string lname)
{
   studentID = a; //assignthe a to studentID
   lastName = lname; //assign lname to lastName
   firstName = fname; //assign fname to firstName
   studentCount++; //increase the studentCount
}
//getStudentCount() method will return the studentCount
int student :: getStudentCount()
{
   return studentCount;
}
//initialize the static data member to 0
int student :: studentCount=0;
//driver program
int main()
{
   //variable declaration
   int n;
   string fname,lname;
   //call the getStudentCount() through student class name
   cout<<endl<<"Number of objects : "<<student :: getStudentCount();
   //input the student ID
   cout<<endl<<"Enter the student ID";
   cin>>n;
   //input the first name and last name
   cout<<endl<<"Enter the first name and last name";
   cin>>fname>>lname;
   //create an object through parameterized constructor
   student s1(n,fname,lname);
   //call the getStudentCount() through student class name
   cout<<endl<<"Number of objects : "<<student :: getStudentCount();
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
C++ program Create a Student class that contains three private data members of stududentID (int), lastName...
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
  • In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName,...

    In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName, GPA (double), and Major. Extend your class for a Student to include classes for Graduate and Undergraduate. o Include a default constructor, a constructor that accepts the above information, and a method that prints out the contents FOR EACH LEVEL of the object, and a method that prints out the contents of the object. o Write a program that uses an array of Students...

  • A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double...

    A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double GPA(=(4.0*NumberOfAs+3.0*NumberOfBs+2.0*NumberOfCs+1.0*NumberOfDs)/( As+Bs+Cs+Ds+Fs)); Public data member, void setFirstName(string name); string getFirstName() const; void printFirstName() const; void getLastName(string name); string getLastName() const; void printLastName() const; void computeGPA(int NumberOfAs,int NumberOfBs,int NumberOfCs,int NumberOfDs,int NumberOfFs); double getGPA() const; double printGPA() const; A destructor and an explicit default constructor initializing GPA=0.0 and checking if GPA>=0.0 by try{}catch{}. Add member function, bool operator<(const Student); The comparison in this function based on...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • c ++ Create a class Book with the data members listed below.     title, which is...

    c ++ Create a class Book with the data members listed below.     title, which is a string (initialize to empty string)     sales, which is a floating point value (as a double, initialize to 0.0) Include the following member functions:     Include a default constructor,     a constructor with parameters for each data member (in the order given above),     getters and setter methods for each data member named in camel-case. For example, if a class had a data...

  • 2- Write a program that has a Person class, a Student class, that is derived from...

    2- Write a program that has a Person class, a Student class, that is derived from Person, and a Faculty class, derived from Person as well, with the following specifications: The Person class has the following members: a. An integer private member variable named SSN. b. Two string protected member variables named firstName and lastName. C. An integer protected member variable named age. d. A constructor that takes SSN, first name, last name, and age as parameters. e. A constructor...

  • Create the Employee class: The Employee class has three attributes: private: string firstName; string lastName; string...

    Create the Employee class: The Employee class has three attributes: private: string firstName; string lastName; string SSN; The Employee class has ▪ a no-arg constructor (a no-arg contructor is a contructor with an empty parameter list): In this no-arg constructor, use “unknown” to initialize all private attributes. ▪ a constructor with parameters for each of the attributes ▪ getter and setter methods for each of the private attributes ▪ a method getFullName() that returns the last name, followed by a...

  • C++ edit: You start with the code given and then modify it so that it does...

    C++ edit: You start with the code given and then modify it so that it does what the following asks for. Create an EmployeeException class whose constructor receives a String that consists of an employee’s ID and pay rate. Modify the Employee class so that it has two more fields, idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, throw an EmployeeException if the hourlyWage is less than $6.00 or over $50.00. Write a program that...

  • public class StudentADT { //1. A Student class should include data attributes including a // student’s...

    public class StudentADT { //1. A Student class should include data attributes including a // student’s name (first, and last), a student number, //an array of 3 assignment grades, and an exam grade. private String firstName; private String lastName; private int studentNumber; private double[] assignmentGrades; private double examGrade; //the constructor StudentADT(firstNm, lastNm, stNo, asn1Grade, asn2Grade, asn3Grade, examGrade) public StudentADT(String firstNm, String lastNm, int stNo, double asn1Grade, double asn2Grade, double asn3Grade, double examGrade) { firstName = firstNm; lastName = lastNm; studentNumber...

  • Write a program

    1. Write a program which have a class named as “Student”, while the data members of the class are,a. char name[10],b. int age;c. string color;d. string gender;e. double cgpa;and the member function “print()”,  is just to display these information.1. You need to create 3 objects of the “Student” class.2. Initialize one object by the default constructor having some static values.3. Initialize the second object by the parameterized constructor, while you need to pass values for initialization.4. Initialize the third object...

  • Create a class hierarchy to be used in a university setting. The classes are as follows:...

    Create a class hierarchy to be used in a university setting. The classes are as follows: The base class is Person. The class should have 3 data members: personID (integer), firstName(string), and lastName(string). The class should also have a static data member called nextID which is used to assign an ID number to each object created (personID). All data members must be private. Create the following member functions: o Accessor functions to allow access to first name and last name...

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
ADVERTISEMENT