COS1512 Assignment03
COS1512 Assignment03
Unique Number:160814
Question 1
#include<iostream>
#include<string>
class Student
{
private:
string name;
int quiz1;
int quiz2;
int midtermExam;
int finalExam;
public:
//Default constructor
Student(){
name= " ";
quiz1=0;
quiz2=0;
midtermExam=0;
finalExam=0;
}
//Mutators (Setters)
void setName(string studentname){
name = studentname;
}
// Accessors (Getters)
string getName()const{
return name;
}
int getQuiz1()const{
return quiz1;
}
int getQuiz2()const{
return quiz2;
}
int getMidtermExam()const{
return midtermExam;
}
int getFinalExam()const{
return finalExam;
}
int main(){
Student studentObj;
return 0;
}
(b) A class is like a blueprint that defines what an object will be like. An
object is an actual thing created from that blueprint and can be used in your
program.
(e) A default constructor gives an object basic initial values without needing
input. An overloaded constructor allows you to create an object with specific
starting values.
(i) The scope resolution operator (::) is used when you want to define or
access something specific from a class, especially when there could be
confusion with other names.
(j) The scope resolution operator (::) is for class or namespace members,
while the dot operator (.) is used to access variables and functions of an
object you’ve created from a class.
(k) A member function belongs to a class and works with that class’s data. An
ordinary function doesn’t belong to any class and works independently.
(l) An abstract data type (ADT) is a type of data that focuses on what it does
(like adding or removing items) rather than how it does it.
(m) You create an ADT by defining what actions can be done with it, usually
through a class, without worrying about the internal details.
(n) ADTs have benefits like making your code more organized, easier to
understand, and reusable without changing how they work inside.
(o) Separate compilation means compiling parts of your program individually
instead of all at once. It’s like assembling a puzzle from smaller pieces.
(p) The advantage of separate compilation is that it saves time, makes large
programs easier to manage, and helps you reuse pieces of code in other
projects.
(q) A derived class is a class that’s based on another class (the base class).
It inherits everything from the base class but can also add its own features.
(r) Inheritance lets you reuse code from one class in another, making it
easier to extend or modify existing classes instead of starting from scratch.
Question 3
Explanation of what is wrong with the given code fragment:
Corrected code:
#include<iostream>
#include<string>
class PersonType
{
private:
string name;
int ID;
string birthday;
public:
PersonType(){
name=" ";
ID=0;
birthday=" ";
}
PersonType(string n, int id, string bd){
name=n;
ID=id;
birthday=bd;
}
string getName()const{
return name;
}
int getID()const{
return ID;
}
string getBirthday()const{
return birthday;
}
};
int main()
{
PersonType family[20];
PersonType newBaby("Anny Dube", 20180912, "2 Sept");
return 0;
}
Question 4
#include <iostream>
using namespace std;
class Date {
private:
int theday;
int themonth;
int theyear;
int monthLength() const;
public:
Date() : theday(14), themonth(9), theyear(1752) {}
Date(int day, int month, int year) : theday(day), themonth(month),
theyear(year) {}
int getDay() const { return theday; }
int getMonth() const { return themonth; }
int getYear() const { return theyear; }
Date& operator++();
Date& operator--();
bool operator<(const Date& d) const;
Date& Date::operator++() {
theday++;
if (theday > monthLength()) {
theday = 1;
themonth++;
if (themonth > 12) {
themonth = 1;
theyear++;
}
}
return *this;
}
Date& Date::operator--() {
theday--;
if (theday < 1) {
themonth--;
if (themonth < 1) {
themonth = 12;
theyear--;
}
theday = monthLength();
}
return *this;
}
int main() {
// Declare a new Date object called d1 (default constructor)
Date d1;
cout << "Default date: " << d1 << endl;
return 0;
}
Question 5
int main() {
std::string testNumber;
std::cout << "Enter the phone number to calculate total charges: ";
std::cin >> testNumber;
PhoneCall theCall(testNumber);
std::ifstream inFile("MyCalls.dat");
if (!inFile) {
std::cerr << "Error opening file!" << std::endl;
return 1;
}
PhoneCall currentCall;
float totalCharges = 0.0f;
int numberOfCalls = 0;
int longestCallLength = 0;
PhoneCall longestCall;
inFile.close();
std::cout << "Total amount spent on calls to " << testNumber << ": " <<
totalCharges << std::endl;
std::cout << "Number of calls to " << testNumber << ": " << numberOfCalls
<< std::endl;
if (numberOfCalls > 0) {
std::cout << "Longest call: " << longestCall << std::endl;
} else {
std::cout << "No calls made to " << testNumber << std::endl;
}
return 0;
}
#include "PhoneCall.h"
// Default constructor
PhoneCall::PhoneCall() : number(""), length(0), rate(0.0f) {}
// Overloaded constructor
PhoneCall::PhoneCall(const std::string& phoneNumber) : number(phoneNumber),
length(0), rate(0.0f) {}
// Destructor
PhoneCall::~PhoneCall() {
// No special action needed
}
// Accessor functions
std::string PhoneCall::getNumber() const {
return number;
}
#define PHONECALL_H
#include <iostream>
#include <string>
class PhoneCall {
private:
std::string number;
int length;
float rate;
public:
PhoneCall();
PhoneCall(const std::string& phoneNumber);
~PhoneCall();
std::string getNumber() const;
int getLength() const;
float getRate() const;
#endif
0123452347 12 3.50
0337698210 9 3.15
0214672341 2 1.75
0337698210 15 3.15
0442389132 8 1.75
0232189726 5 3.50
0124395623 6 3.50
0337698210 2 3.15
0337698210 5 3.15
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
class Student {
private:
string name;
int quiz1;
int quiz2;
int midtermExam;
int finalExam;
public:
// Default constructor
Student() : name(" "), quiz1(0), quiz2(0), midtermExam(0), finalExam(0) {}
// Mutators (Setters)
void setName(string studentname) { name = studentname; }
void setQuiz1(int marks) { quiz1 = marks; }
void setQuiz2(int marks) { quiz2 = marks; }
void setMidtermExam(int marks) { midtermExam = marks; }
void setFinalExam(int marks) { finalExam = marks; }
// Accessors (Getters)
string getName() const { return name; }
int getQuiz1() const { return quiz1; }
int getQuiz2() const { return quiz2; }
int getMidtermExam() const { return midtermExam; }
int getFinalExam() const { return finalExam; }
if (name.empty()) {
return in; // End of file or empty line
}
student.setName(name);
student.setQuiz1(quiz1);
student.setQuiz2(quiz2);
student.setMidtermExam(midtermExam);
student.setFinalExam(finalExam);
return in;
}
int main() {
Student students[MAX_STUDENTS];
int count = 0;
ifstream inFile("Student.dat");
if (!inFile) {
cerr << "Error opening file!" << endl;
return 1;
}
string line;
while (getline(inFile, line) && count < MAX_STUDENTS) {
stringstream ss(line);
Student tempStudent;
if (ss >> tempStudent) {
students[count++] = tempStudent;
} else {
cerr << "Error processing line: " << line << endl;
}
}
inFile.close();
if (count == 0) {
cerr << "No student data available." << endl;
return 1;
}
double totalAverage = 0;
if (count > 0) {
cout << "Class average: " << (totalAverage / count) << endl;
}
return 0;
}
Question 7
#include <iostream>
#include <string>
using namespace std;
class Student {
protected:
string name;
string studentNumber;
string address;
string degree;
public:
Student(string n, string sn, string addr, string deg)
: name(n), studentNumber(sn), address(addr), degree(deg) {}
public:
PostgradStd(string n, string sn, string addr, string deg, string diss)
: Student(n, sn, addr, deg), dissertation(diss) {}
int main() {
// Test class Student
cout << "Testing Student class:" << endl;
Student s("Mary Mbeli", "12345678", "Po Box 16, Pretoria, 0818", "BSc");
return 0;
}