100% Zufriedenheitsgarantie Sofort verfügbar nach Zahlung Sowohl online als auch als PDF Du bist an nichts gebunden
logo-home
COS1512 Assignment 3 (COMPLETE ANSWERS) 2024 (160814) - DUE 23 August 2024 2,70 €   In den Einkaufswagen

Prüfung

COS1512 Assignment 3 (COMPLETE ANSWERS) 2024 (160814) - DUE 23 August 2024

 144 mal angesehen  17 mal verkauft
  • Kurs
  • Hochschule
  • Book

COS1512 Assignment 3 (COMPLETE ANSWERS) 2024 (160814) - DUE 23 August 2024 ;100% TRUSTED workings, explanations and solutions. for assistance Whats-App.......0.6.7..1.7.1..1.7.3.9 .......... Question 1 Consider the following structure used to keep record of a student’s scores: struct Student ...

[ Mehr anzeigen ]

vorschau 4 aus 38   Seiten

  • 23. juni 2024
  • 38
  • 2023/2024
  • Prüfung
  • Fragen & Antworten
avatar-seller
COS1512
Assignment 3 2024
Detailed Solutions, References & Explanations

Unique number: 160814

Due Date: 23 August 2024
QUESTION 1




Terms of use
By making use of this document you agree to:
• Use this document as a guide for learning, comparison and reference purpose,
• Not to duplicate, reproduce and/or misrepresent the contents of this document as your own work,
• Fully accept the consequences should you plagiarise or misuse this document.

Disclaimer
Extreme care has been used to create this document, however the contents are provided “as is” without
any representations or warranties, express or implied. The author assumes no liability as a result of
reliance and use of the contents of this document. This document is to be used for comparison, research
and reference purposes ONLY. No part of this document may be reproduced, resold or transmitted in any
form or by any means.

,4. Solution to Assignment
Question 1 max of 5 marks

Discussion:
For this question, you had to convert the struct Student into a class. There is essentially only a slight
difference between a struct and a class. A class is the same as a struct (i.e. a struct may also
contain both member variables and member functions just like a class, even though the examples in
Savitch do not show that) except that, by default, all members are inaccessible to the general user of the
class. This means that all members of a struct are by default public, and all members of a class are
by default private. Therefore, we have to specify that the member functions are public. (As an
exercise, omit the public keyword from the class and recompile it.) While there are situations where a
struct is more suitable, as a rule, you should use classes rather than structs. In general, it is preferable
to use classes, as classes offer better protection.

An object encapsulates or combines data and operations on that data into a single unit. In C++, the
mechanism that allows you to combine data and the operations on that data in a single unit is called a
class.

As name, quiz1, quiz2, midtermExam and finalExam are private member variables (see page
549 of Savitch 6th edition/ page 581 of Savitch 7th edition/ pages 573-582 of Savitch, 8th edition/pages
589-595 of Savitch, 9th edition) of class Student, they cannot be accessed directly in the main()
function. As a result, public member functions getQuiz1(), getQuiz2(), getMidtermExam(),
getFinalExam() and Term() are used to access these member variables in order to determine their
values. These functions are known as accessor functions, while setName(), setQuiz1(), setQuiz2(),
setMidtermExam()and setFinalExam()are mutator functions. (Study pages 553-554 of Savitch, 6th
edition/ pages 585-586 of Savitch 7th edition/ pages 581-582 of Savitch, 8th edition/ pages 597-598 of
Savitch, 9th edition).

Mutator functions are used to change or modify member variables of an object. The parameter of the
mutator function typically indicates the value according to which the member variable should be changed.
For example, the mutator function setQuiz1()below modifies member variable quiz1 to q1:
void Student::setQuiz1(int q1)
{
quiz1 = q1;
}

Note the prototypes for member functions:
string getName()const;
int getQuiz1()const;
int getQuiz2()const;
int getMidtermExam()const;
int getFinalExam()const;

These member function are accessors - hence the const keyword at the end of the function definition.
Note that a member function that does not have the const keyword at the end of it could mutate (change
or modify) the state of the object. Although member function calcAverage() is not an accessor, it should
not modify the object, and therefore the prototype for member function calcAverage() also has the
const keyword at the end of the function definition:
int calcAverage()const;

,Program listing:
#include <iostream>
#include <string>
using namespace std;

//Class declare
class Student
{
public:
Student();
void setName(string n);
void setQuiz1(int q1);
void setQuiz2(int q2);
void setMidtermExam(int m);
void setFinalExam(int f);
string getName()const;
int getQuiz1()const;
int getQuiz2()const;
int getMidtermExam()const;
int getFinalExam()const;
int calcAverage()const;
private:
string name;
int quiz1;
int quiz2;
int midtermExam;
int finalExam;
};

//Implementation of member functions for class Student
Student::Student()
{
name = " ";
quiz1 = 0;
quiz2 = 0;
midtermExam = 0;
finalExam = 0;
}

void Student::setName(string n)
{
name = n;
}

void Student::setQuiz1(int q1)
{
quiz1 = q1;
}

void Student::setQuiz2(int q2)
{
quiz2 = q2;
}

void Student::setMidtermExam(int m)
{
midtermExam = m;
}

, void Student::setFinalExam(int f)
{
finalExam = f;
}
string Student::getName()const
{
return name;
}

int Student::getQuiz1()const
{
return quiz1;
}

int Student::getQuiz2()const
{
return quiz2;
}

int Student::getMidtermExam()const
{
return midtermExam;
}

int Student::getFinalExam()const
{
return finalExam;
}

int Student::calcAverage()const
{
return int((quiz1 * 10 + quiz2 * 10)/2 * 0.25 + midtermExam * 0.25
+ finalExam * 0.5);
}
//Main function of application
int main()
{
Student aStudent;
string n;
int q1,q2,m,f;

// Obtain student detail
cout << "Please enter student's name: ";
getline(cin, n);
aStudent.setName(n);
cout << "Enter mark for quiz 1: ";
cin >> q1;
aStudent.setQuiz1(q1);
cout << "Enter mark for quiz 2: ";
cin >> q2;
aStudent.setQuiz2(q2);
cout << "Enter mark for midterm exam: ";
cin >> m;
aStudent.setMidtermExam(m);
cout << "Enter mark for final exam: ";
cin >> f;
aStudent.setFinalExam(f);

Alle Vorteile der Zusammenfassungen von Stuvia auf einen Blick:

Garantiert gute Qualität durch Reviews

Garantiert gute Qualität durch Reviews

Stuvia Verkäufer haben mehr als 700.000 Zusammenfassungen beurteilt. Deshalb weißt du dass du das beste Dokument kaufst.

Schnell und einfach kaufen

Schnell und einfach kaufen

Man bezahlt schnell und einfach mit iDeal, Kreditkarte oder Stuvia-Kredit für die Zusammenfassungen. Man braucht keine Mitgliedschaft.

Konzentration auf den Kern der Sache

Konzentration auf den Kern der Sache

Deine Mitstudenten schreiben die Zusammenfassungen. Deshalb enthalten die Zusammenfassungen immer aktuelle, zuverlässige und up-to-date Informationen. Damit kommst du schnell zum Kern der Sache.

Häufig gestellte Fragen

Was bekomme ich, wenn ich dieses Dokument kaufe?

Du erhältst eine PDF-Datei, die sofort nach dem Kauf verfügbar ist. Das gekaufte Dokument ist jederzeit, überall und unbegrenzt über dein Profil zugänglich.

Zufriedenheitsgarantie: Wie funktioniert das?

Unsere Zufriedenheitsgarantie sorgt dafür, dass du immer eine Lernunterlage findest, die zu dir passt. Du füllst ein Formular aus und unser Kundendienstteam kümmert sich um den Rest.

Wem kaufe ich diese Zusammenfassung ab?

Stuvia ist ein Marktplatz, du kaufst dieses Dokument also nicht von uns, sondern vom Verkäufer iStudy. Stuvia erleichtert die Zahlung an den Verkäufer.

Werde ich an ein Abonnement gebunden sein?

Nein, du kaufst diese Zusammenfassung nur für 2,70 €. Du bist nach deinem Kauf an nichts gebunden.

Kann man Stuvia trauen?

4.6 Sterne auf Google & Trustpilot (+1000 reviews)

45.681 Zusammenfassungen wurden in den letzten 30 Tagen verkauft

Gegründet 2010, seit 14 Jahren die erste Adresse für Zusammenfassungen

Starte mit dem Verkauf
2,70 €  17x  verkauft
  • (0)
  Kaufen