Answer to Question #309773 in C++ for Divija

Question #309773

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'.

1
Expert's answer
2022-03-11T07:24:44-0500
#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();
}	

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment