Header Ads

WAP in C++ to create database of the following items: Name of the student (String), Roll number of the student (int), Height of the student (cm), Weight of the student (kg/gms)

1) Create a Constructor to initialize values

2) Create display () function to display the details

3) Illustrate the use of copy constructor

4) Also implement the concept of destructor. 




Algorithm :

This program defines a Student class with a constructor, a display function, a copy constructor, and a destructor. It creates an object of the Student class and displays its details using the display function. Then it creates a copy of the first object using the copy constructor, displays its details, and calls the destructor for both objects.

Here's the algorithm for this program:
  1. Define a Student class with private member variables for name, rollNumber, height, and weight, and public member functions for constructor, display function, copy constructor, and destructor.
  2. Implement the constructor to initialize the member variables.
  3. Implement the display function to display the member variables.
  4. Implement the copy constructor to copy the member variables of an object to another object.

  5. Implement the destructor to display a message when an object is destroyed.
  6. In the main function, create an object of the Student class using the constructor and display its details using the display function.
  7. Create a copy of the first object using the copy constructor and display its details using the display function.
  8. Call the destructor explicitly for the first object.
  9. The program will implicitly call the destructor for the second object when it goes out of scope.


Code:

#include <iostream>
#include <string>
using namespace std;

class Student {
private:
string name;
int rollNumber;
float height;
float weight;

public:
// Constructor to initialize values
Student(string name, int rollNumber, float height, float weight) {
this->name = name;
this->rollNumber = rollNumber;
this->height = height;
this->weight = weight;
}

// Display function to display the details
void display() {
cout << "Name: " << name << endl;
cout << "Roll Number: " << rollNumber << endl;
cout << "Height: " << height << " cm" << endl;
cout << "Weight: " << weight << " kg" << endl;
}

// Copy constructor
Student(const Student& obj) {
name = obj.name;
rollNumber = obj.rollNumber;
height = obj.height;
weight = obj.weight;
}

// Destructor
~Student() {
cout << "Destructor called for " << name << endl;
}
};

int main() {
// Create a student object using constructor
Student student1("Abhay", 123, 180, 70);

// Display the details
cout << "Details of student1:" << endl;
student1.display();

// Create a copy of student1 using copy constructor
Student student2 = student1;

// Display the details of the copied object
cout << "\nDetails of student2:" << endl;
student2.display();

// Call the destructor explicitly for student1
student1.~Student();

// Call the destructor implicitly for student2
return 0;
}


Output:

Details of student1:
Name: Abhay
Roll Number: 123
Height: 180 cm
Weight: 70 kg

Details of student2:
Name: Abhay
Roll Number: 123
Height: 180 cm
Weight: 70 kg
Destructor called for Abhay
Destructor called for Abhay
Destructor called for Abhay


Explanation:

This C++ program demonstrates the concept of constructors, copy constructors, and destructors using a class called Student.
The Student class has private member variables name, rollNumber, height, and weight, and public member functions to initialize these values using a constructor, display the values, and copy the object using a copy constructor. The class also has a destructor which is called when the object is destroyed.
The main() function first creates an object of the Student class using the constructor, initializes it with the values "Abhay", 123, 180, and 70, and displays the details of the object using the display() function.
Next, the program creates a copy of the object student1 using the copy constructor and stores it in student2. The details of student2 are displayed using the display() function.
Then, the destructor of student1 is called explicitly using the ~Student() function. Finally, the destructor of student2 is called implicitly when the program exits the main() function.
Overall, this program demonstrates how constructors, copy constructors, and destructors work in C++ classes.





Related Links:




sign in process initialization failure fix

define a class to declare a character array of size 10 accept the character into the array

design a java program to create to display your name, branch with your college name?

write a java program to display your name and roll number on the terminal

define a class to declare a character array of size 10

define a class to declare a character array of size ten, accept the character into the array and perform the following: count the number of uppercase letters in the array and print. count the number of vowels in the array and print.



Post a Comment

0 Comments