212: Build a class representing a university student with private data members for name, ID, and a static data member for the total number of students. Implement getter and setter functions for name and ID, and a static getter function to retrieve the total number of students. Use default constructors

 

// Build  a  class  representing  a  university  student  with  private  data  members  for
// name, ID, and a static data member for the total number of students. Implement
// getter  and  setter  functions  for  name  and  ID,  and  a  static  getter  function  to
// retrieve the total number of students. Use default constructors

#include <iostream>
using namespace std;
class Student
{
private:
    static unsigned int count;
    string name, id;

public:
    // constructor
    // Student() : name(""), id("") { count++; }
    // Student(string x, string y) : name(x), id(y) { count++; }

    // set data
    void setData();

    // print
    void printStudent();
    static int printTotal_students();
};
unsigned int Student ::count = 0;
int Student ::printTotal_students() { return count; }
void Student ::setData()
{
    cout << "Enter name: ";
    getline(cin >> ws, name);
    cout << "Enter id: ";
    getline(cin >> ws, id);
    count++;
}
void Student ::printStudent()
{
    cout << "Name: " << name << endl;
    cout << "Id: " << id << endl;
}

// user-defined functions
string input(string x)
{
    string temp;
    cout << x;
    cin >> temp;
    return temp;
}

int main()
{
    Student students[10];

    // input students
    for (int i = 0; i < 10; i++)
    {
        char temp;
        cout << "Enter # to exit or any other key to continue: ";
        cin >> temp;
        if (temp == '#')
        {
            break;
        }
        students[i].setData();
        cout << endl;
    }
    system("cls");
    // print students
    for (int i = 0; i < Student::printTotal_students(); i++)
    {
        cout << "Student # " << i + 1 << ':' << endl;
        students[i].printStudent();
        cout << endl;
    }

    cout << "Total no. of Students: " << Student::printTotal_students() << endl;
    return 0;
}


Comments

Popular posts from this blog

88: Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: // Percentage >= 90% : Grade A Percentage >= 80% : Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage >= 40% : Grade E Percentage < 40% : Grade F

205: Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year. Write a program to create a catalog of books by taking user input and display books published after a certain year.

15: Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. -_-_-_-_-_-_-_-_-(line with spaces input concept)-_-_-_-_-_-_-_-_