Class Creation
#include <stdafx.h>
#include <iostream>
#include <string>
using namespace std;
class Student
{
public: /* This is an access modifier. A public member can be accessed from outside the class anywhere within the scope of the class object.*/
string name; /* These are attributes of a class. */
int roll_number;
int mark;
void display(string name) /* This is a member function of the class. Receives a string parameter and displays it. */
{
cout<< name << endl;
}
};
int main()
{
Student stud1; /* Declare the object stud1 of type Student. */
Student stud2; /* Declare the object stud2 of type Student. */
// stud 1 specification
stud1.name = "AAA";
stud1.roll_number = 20;
stud1.mark = 95;
// stud 2 specification
stud2.name = "BBB";
stud2.roll_number = 30;
stud2.mark = 98;
stud1.display("student 1: " +stud1.name); // calls the member function display of the class Student by passing a string parameter.
stud2.display("student 2: " +stud2.name);
return 0;
}
Output:
student 1: AAA
student 2: BBB