Question
Objective: Learn how to -- use static data member -- allocate memory dynamically Problem: Define a class called Student, which has three private data members:
Objective: Learn how to
-- use static data member
-- allocate memory dynamically
Problem: Define a class called Student, which has three private data members: char *name, int age, and static int count, and four public member functions: a constructor, a destructor, setName and printName. In the constructor, initialize the name to NULL and age = 0, increment the static variable count by 1 and output the message Object X created where X is a number stored in the variable count. In the destructor, if the name is not NULL output Destructor for name, delete name, otherwise, output Destructor for object X; in both cases, decrement count by 1. In the function setName, use new to allocate memory for the data members dynamically and assign the age. In the main functions, you first prompt the user to enter the number of students and then use new to create an array of objects. Since the total number of names is given you must use for loop to prompt user to enter name and age (you can use a char array and an int to pick up input data without calling strtok). Make sure that the number of valid names you entered should be exact the same as the number you entered at the beginning. You may assume that the user enters correct names and ages.
Requirements: In the function setName, you must use new operator to dynamically allocate exact spaces for name. You must use three files.
Suggested screen design:
Enter the number of students: 3
Object 1 created.
Object 2 created.
Object 3 created.
Enter student name and age for object 1: Linda 23
Enter student name and age for object 2: Jane 32
Enter student name and age for object 3: Jim 21
There are 3 persons on the list.
Name Age
Linda 23
Jane 32
Jim 21
Destructor for Jim
Destructor for Jane
Destructor for Linda
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started