Write a C++ program to make a structure of a student consisting of integer age, char name and
structure roll number that further divides into department, session, registration number and
degree. Set and display all the values from main function and display the record on screen to
present the access of structure within structure. You have to by access structure using pointer
type variable of structure.
#include <iostream>
struct RollNumber
{
const char* department;
const char* session;
int registrationNumber;
const char* degree;
};
struct Student
{
const char* name;
int age;
RollNumber* rollNumber;
};
int main()
{
RollNumber* rollNumber = new RollNumber;
rollNumber->department = "Mathematics";
rollNumber->session = "Spring 2022";
rollNumber->registrationNumber = 284735;
rollNumber->degree = "Bachelor";
Student* student = new Student;
student->age = 22;
student->name = "John Wales";
student->rollNumber = rollNumber;
std::cout << "Name: " << student->name << std::endl;
std::cout << "Age: " << student->age << std::endl;
std::cout << "Roll number: " << std::endl;
std::cout << "* Department: " << student->rollNumber->department << std::endl;
std::cout << "* Session: " << student->rollNumber->session << std::endl;
std::cout << "* Registration number: " << student->rollNumber->registrationNumber << std::endl;
std::cout << "* Degree: " << student->rollNumber->degree << std::endl;
delete student;
delete rollNumber;
return 0;
}
Comments
Leave a comment