Tuesday, July 25, 2017

C++ program with a Class

Demonstration of C++ program with a "Student" Class

#include<iostream>
#include<string.h>
using namespace std;

class student
{
  int rollno;
  float percentage;
  char name[10];

  public:
      void getdata(int a, float b, char* n)
      {
  rollno = a;
  percentage = b;
  strcpy(name,n);
      }
      void putdata()
      {
  cout << "Roll no = " << rollno << "\n";
  cout << "Percentage = " << percentage << "\n";
  cout << "Name = " << name << "\n\n";
      }
};

int main()
{
  student s1, s2;

  s1.getdata(101,80.85,"ABC");
  s1.putdata();

  s1.getdata(102,95,"XYZ");
  s1.putdata();

  return 0;
}

Output of program

Roll no = 101
Percentage = 80.85
Name = ABC

Roll no = 102
Percentage = 95
Name = XYZ

No comments:

Post a Comment