100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
COS1512EXAM PACK 2025 {DETAILED QUESTIONS AND ANSWERS } R53,47
Add to cart

Exam (elaborations)

COS1512EXAM PACK 2025 {DETAILED QUESTIONS AND ANSWERS }

 10 views  0 purchase

COS1512EXAM PACK 2025 {DETAILED QUESTIONS AND ANSWERS }

Preview 4 out of 118  pages

  • November 14, 2024
  • 118
  • 2024/2025
  • Exam (elaborations)
  • Questions & answers
All documents for this subject (40)
avatar-seller
MasterVincent
lOMoAR cPSD| 49343224




COS1512/202/1/2018




Downloaded by Vincent master

, lOMoAR cPSD| 49343224




2




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(),


Downloaded by Vincent master (VINCENTKYALO920@GMAIL.COM)

, lOMoAR cPSD| 49343224




3 COS1512/202/1/2018



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;



Downloaded by Vincent master

, lOMoAR cPSD| 49343224




4




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()


Downloaded by Vincent master (VINCENTKYALO920@GMAIL.COM)

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through EFT, credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying this summary from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller MasterVincent. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy this summary for R53,47. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

50843 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy summaries for 14 years now

Start selling
R53,47
  • (0)
Add to cart
Added