logo

Crowdly

Browser

Add to Chrome

A structure in C is a user defined data type that can be used to group multiple ...

✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.

A structure in C is a user defined data type that can be used to group multiple values under one name. These individual values are called data members and they have different names and possibly different types.

A structure in C++ is a more complex data type that provides facilities to easily implement the concepts of data abstraction and encapsulation [that will be explained in future lectures]. In addition to data members, C++ structures can also contain type declarations and functions. Member functions of a C++ structure can be used by programmers to initialize, to retrieve, and modify the state of the data members of a variable of that structure type. For example, we can define a type Student like this:

#include <string>

struct Student {

std::string name; // string is C++ standard library type declared in <string> that represents a

// variable-length sequence of characters. This class allows us to handle C-style strings

// similar to values of type int

int id;

double gpa;

Student(std::string const& a_name, int an_id, double a_gpa) : name{a_name}, id{an_id}, gpa{a_gpa} { }

void GPA(double new_gpa) { gpa = new_gpa; } // 1st overloaded member function GPA called modifier member function

double GPA() const { return gpa; } // 2nd overloaded member function GPA called accessor member function

};

A member function with the same name as the structure type is called a constructor. The purpose of a constructor is to initialize the data members of an object.

The first overloaded member function GPA is called a modifier because it changes the value of data member gpa. The second overloaded member function GPA is called an accessor because it retrieves the current value of data member gpa.

What is the exact output written to standard output by the following code fragment?

Student s {"Bart", 98, 2.1};

std::cout << s.name << ' ' << s.id << ' ' << s.GPA();

More questions like this

Want instant access to all verified answers on distance3.sg.digipen.edu?

Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!

Browser

Add to Chrome