2.Assign and print the roll number, phone number and address of two students having
names "Sam" and "John" respectively by creating two objects of the class 'Student'.
#include<iostream>
#include<string>
using namespace std;
class Student
{
string name;
int rollNo;
int phoneNo;
string address;
public:
Student() {}
Student(string _name, int _rollNo, int _phoneNo, string _address)
:name(_name),rollNo(_rollNo), phoneNo(_phoneNo), address(_address){}
void Assign()
{
cout << "Please, enter a name of student: ";
cin >> name;
cout << "Please, enter a rollNo of student: ";
cin >> rollNo;
cout << "Please, enter a phoneNo of student: ";
cin >> phoneNo;
cout << "Please, enter an address of student: ";
cin.ignore(256, '\n');
getline(cin, address, '\n');
}
void Print()
{
cout << "\nName of student is " << name;
cout << "\nRollNo of student is " << rollNo;
cout << "\nPhoneNo of student is " << phoneNo;
cout << "\nAddress of student is " << address;
}
};
int main()
{
Student a("Sam", 1, 123456789, "Apple avenue,12");
Student b("John", 2, 987654321, "Cherry street,14");
Student c;
c.Assign();
a.Print();
b.Print();
c.Print();
}
Comments
Leave a comment