Write a class student with following data members and methods.
Private:
1. stdID //A string that hold the ID of student
2. stdName //A string that hold the name of student
3. dept //A string that hold the department name of student
Public:
1. Default Constructor initialize all member variables with null value
2. Overloaded Constructor1 (stdID= 12345, stdName = Ali Hassan)
3. Overloaded Constructor2 (stdID= 23456, std, Name = Sheraz Depp, dept = Computer
Science)
4. Overloaded Constructor3 (stdName = Fahad abbas , dept = Computer Science)
5. inputData Input from user using this pointer for all members
6. displayData method that display data using this pointer
Now in main function create three objects (obj1, obj2, obj3) and each call different constructors.
Now create obj4 and initialize it with obj3 using copy constructor.
Now dynamically create 3 other objects and initialize them with any data of your choice. Show
your results on console.
Â
#include <iostream>
using namespace std;
class Student{
  private:
    string stdID;
    string stdName;
    string dept;
  Â
  public:
    Student(){
     Â
    }
    Student(int id,string name){
      stdID=id;
      stdName=name;
    }
    Student(string id,string name,string d){
      stdID=id;
      stdName=name;
      dept=d;
    }
    Student(string name,string d){
      stdName=name;
      dept=d;
    }
    void inputData(){
      cout<<"\nEnter student ID: ";
      cin>>this->stdID;
      cout<<"\nEnter student name: ";
      cin>>this->stdName;
      cout<<"\nEnter department: ";
      cin>>this->dept;
    }
    void displayData(){
      cout<<"\nStudent ID:"<<this->stdID;
      cout<<"\nStudent Name: "<<this->stdName;
      cout<<"\nDepartment: "<<this->dept;
    }
};
int main()
{
  Student obj1("12345","Ali Hassan");
  Student obj2("23456","Sheraz Depp","Computer Science");
  Student obj3("Fahad abbas","Computer Science");
  Student obj4=obj3;
  obj1.displayData();
  obj2.displayData();
  obj3.displayData();
  obj4.displayData();
  Student *s1=new Student();
  Student *s2=new Student();
  Student *s3=new Student();
  s1->inputData();
  s1->displayData();
  s2->inputData();
  s2->displayData();
  s3->inputData();
  s3->displayData();
  return 0;
}
Comments
there is a error in this program please coreect it
Leave a comment