C++: Classes And Objects 4

Examples:

Question:1
C++ to declare a class named student with attributes as roll, name and age. Take input for the details and display them. (For one student(single object))(Definition of member functions outside the class)
Sol:

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

class student
{
private:
       int roll;
       char name[20];
       float age;
public:
    void read();
    void show();
};
void student::read()
{
    cout<<"Enter roll,name and age ";
    cin>>roll>>name>>age;
}
void student::show()
{
    cout<<"roll "<<roll<<" name "<<name<<" age "<<age<<endl;
}
int main()
{
    student s1;
    s1.read();
    s1.show();
    return(0);
}
/* Output */

Enter roll,name and age 101
abc
15
roll 101 name abc age 15

Question:2
C++ to declare a class named student with attributes as roll, name and age. Take input for the details and display them. (For 3 students (multiple object))(Definition of member functions outside the class)
Sol:

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

class student
{
private:
       int roll;
       char name[20];
       float age;
public:
    void read();
    void show();
};
void student::read()
{
    cout<<"Enter roll,name and age ";
    cin>>roll>>name>>age;
}
void student::show()
{
    cout<<"roll "<<roll<<" name "<<name<<" age "<<age<<endl;
}
int main()
{
    student s1,s2,s3;
    s1.read(); s2.read(); s3.read();
    s1.show(); s2.show(); s3.show();
    return(0);
}
/* Output */

Enter roll,name and age 101 amit 14
Enter roll,name and age 102 sumit 15
Enter roll,name and age 103 kapil
20
roll 101 name amit age 14
roll 102 name sumit age 15
roll 103 name kapil age 20

Question:3
C++ to declare a class named student with attributes as roll, name and age. Take input for the details and display them. (For 10 students(Array type objects))(Definition of member functions outside the class)
Sol:

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

class student
{
private:
       int roll;
       char name[20];
       float age;
public:
    void read();
    void show();
};
void student::read()
{
    cout<<"Enter roll,name and age ";
    cin>>roll>>name>>age;
}
void student::show()
{
    cout<<"roll "<<roll<<" name "<<name<<" age "<<age<<endl;
}
int main()
{
    student s[10];
    int i;
    //input
    for(i=0;i<10;i++)
    {
        s[i].read();
    }
    //display
    for(i=0;i<10;i++)
    {
        s[i].show();
    }
    return(0);
}
/* Output */

Enter roll,name and age 101 aaa 12
Enter roll,name and age 102 bbb 14
Enter roll,name and age 103 ccc 15
Enter roll,name and age 104 ddd 21
Enter roll,name and age 105 eee 14
Enter roll,name and age 106 fff 23
Enter roll,name and age 107 ggg 15
Enter roll,name and age 108 hhh 17
Enter roll,name and age 109 iii 21
Enter roll,name and age 110 jjj 22
roll 101 name aaa age 12
roll 102 name bbb age 14
roll 103 name ccc age 15
roll 104 name ddd age 21
roll 105 name eee age 14
roll 106 name fff age 23
roll 107 name ggg age 15
roll 108 name hhh age 17
roll 109 name iii age 21
roll 110 name jjj age 22