Assign and print the ID number, phone
number, and address of three students. The object names are "Sam", "John", and "Alex". Use the information provided below.
Use the principle of User Input, The console is asking the user about the name of the student and then the computer will return the information.
Use a Constructor to code the initialized the values upon creating the object. (Inside or Outside class defined)
Student Information:
Sam
ID No: 2021456
Phone No: 656678
Address: Cupang
John
ID No: 2021457
Phone No: 898889
Address: Alabang
Alex
ID No: 2021458
Phone No: 343456
Address: Tuansan
#include<iostream>
using namespace std;
class Student{
private:
int id_no, phone;
string address;
public:
Student(){
}
Student(int i, int p, string a){
id_no = i;
phone = p;
address = a;
}
void display(){
cout<<"ID No: "<<id_no<<"\nPhone No: "<<phone<<"\nAddress: "<<address<<endl;
}
};
int main(){
Student st[3];
string name[3] = {"Sam", "John","Alex"};
for(int i=0; i<3; i++){
cout<<name[i]<<endl;
cout<<"Enter ID No: ";
int id;
cin>>id;
cout<<"Enter Phone No: ";
int phone;
cin>>phone;
cout<<"Enter Address: ";
string Address;
cin>>Address;
Student stu(id, phone, Address);
st[i] = stu;
}
cout<<"The details are:\n";
for(int i=0; i<3; i++){
st[i].display();
}
}
Comments
Leave a comment