Array Type Object with new and delete
Example:1
Write a C++ program to declare a class named employee with attributes as employee number, employee name, and basic salary. Take input for the details of “n” employees and display them. Allocate the memory dynamically.
Sol:
//1st method #include<iostream> #include<conio.h> using namespace std; class employee { private: int empno; char name[20]; float sal; public: void read(); void show(); }; void employee::read() { cout<<"Enter empno, name and salary "; cin>>empno>>name>>sal; } void employee::show() { cout<<"empno "<<empno<<" name "<<name<<" sal "<<sal<<endl; } int main() { //pointer type object int n,i; char ch; do { cout<<"Enter total employees "; cin>>n; //allocation of memory dynamically employee *p=new employee[n]; //to check if(p==NULL) { cout<<"memory allocation failed"<<endl; exit(1); } //input for(i=0;i<n;i++) { p[i].read(); } //display for(i=0;i<n;i++) { p[i].show(); } delete []p; cout<<"Like to cont ... (y/n) "; cin>>ch; }while(ch=='y' || ch=='Y'); return(0); }
//2nd method #include<iostream> #include<conio.h> using namespace std; class employee { private: int empno; char name[20]; float sal; public: void read(); void show(); }; void employee::read() { cout<<"Enter empno, name and salary "; cin>>empno>>name>>sal; } void employee::show() { cout<<"empno "<<empno<<" name "<<name<<" sal "<<sal<<endl; } int main() { //pointer type object int n,i; char ch; do { cout<<"Enter total employees "; cin>>n; //allocation of memory dynamically employee *p=new employee[n]; //to check if(p==NULL) { cout<<"memory allocation failed"<<endl; exit(1); } //input for(i=0;i<n;i++) { p->read(); p++; } p=p-n; //display for(i=0;i<n;i++) { p->show(); p++; } p=p-n; delete []p; cout<<"Like to cont ... (y/n) "; cin>>ch; }while(ch=='y' || ch=='Y'); return(0); }
Output:
Enter total employees 2
Enter empno, name and salary 101
sumit
45000
Enter empno, name and salary 102
kapil
65000
empno 101 name sumit sal 45000
empno 102 name kapil sal 65000
Like to cont … (y/n) n
Example:2
Write a C++ program to declare a class named student with attributes as student code , student name and percentage of marks. Take input for the details of “n” students and display them. Allocate the memory dynamically.
Sol:
#include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; class student { private: int roll; char name[20]; float per; public: void read(); void show(); }; void student::read() { cout<<"Enter roll, name and per "; cin>>roll>>name>>per; } void student::show() { cout<<"roll "<<roll<<" name "<<name<<" per "<<per<<endl; } int main() { //pointer type object int n,i; char ch; do { cout<<"Enter total students "; cin>>n; //allocation of memory dynamically student *p=new student[n]; //to check if(p==NULL) { cout<<"memory allocation failed"<<endl; exit(1); } //input for(i=0;i<n;i++) { p[i].read(); } //display for(i=0;i<n;i++) { p[i].show(); } delete []p; cout<<"Like to cont ... (y/n) "; cin>>ch; }while(ch=='y' || ch=='Y'); return(0); }
//2nd method #include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; class student { private: int roll; char name[20]; float per; public: void read(); void show(); }; void student::read() { cout<<"Enter roll, name and per "; cin>>roll>>name>>per; } void student::show() { cout<<"roll "<<roll<<" name "<<name<<" per "<<per<<endl; } int main() { //pointer type object int n,i; char ch; do { cout<<"Enter total students "; cin>>n; //allocation of memory dynamically student *p=new student[n]; //to check if(p==NULL) { cout<<"memory allocation failed"<<endl; exit(1); } //input for(i=0;i<n;i++) { p->read(); p++; } p=p-n; //display for(i=0;i<n;i++) { p->show(); p++; } p=p-n; delete []p; cout<<"Like to cont ... (y/n) "; cin>>ch; }while(ch=='y' || ch=='Y'); return(0); }
Output:
Enter total students 2
Enter roll, name and per 101 abc 98
Enter roll, name and per 102 xyz 99
roll 101 name abc per 98
roll 102 name xyz per 99
Like to cont … (y/n)