CLASS STUDENT( USING ALL TYPES OF CONSTRUCTOR )

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class student
 { private:
    int id;
    char name[30];
    int marks[5];
    int avg;
   public:
    student();
    student(student &si);
    void readstudent();
    void calcavg();
    void display();
 };

void student::student()         //Default Constructor (Outline)
 { id=0;
   strcpy(name,"AAA");
   for(int i=0;i<5;i++)
      { marks[i]=0;
      }
   avg=0;
 }

void student::student(student &si) // Copy constructor (outlined)
 { id=si.id;
   strcpy(name,si.name);
   for(int i=0;i<5;i++)
      { marks[i]=si.marks[i];
      }
   avg=si.avg;
 }

void student::readstudent()
 { cout<<"Enter Student ID ";
   cin>>id;
   cout<<"Enter Student name ";
   gets(name);
   cout<<"Enter students' marks in 5 subjects ";
   for(int i=0;i<5;i++)
      { cin>>marks[i];
      }
 }

void student::calcavg()
 { for(int i=0;i<5;i++)
      { avg+=marks[i];
      }
   avg=avg/5;
 }

void student::display()
     { cout<<"Name :";
       puts(name);
       cout<<"Id : \t "<<id;
       cout<<"\n Average marks = "<<avg;
     }
void main()
{ clrscr();
  student s1,s2;
  s1.readstudent();
  s1.calcavg();
  student(s2)=s1;
  s1.display();
  s2.display();
  getch();
}

Comments

Popular Posts