Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
Samenvatting

COS2614_ Complete Summary (Programming: Contemporary Concepts)

Beoordeling
-
Verkocht
-
Pagina's
92
Geüpload op
06-10-2021
Geschreven in
2021/2022

COS2614_ Complete Summary (Programming: Contemporary Concepts). Note UML class have 3 compartments: Class name, data members and then member functions When you have a class in another class. It’s better to start of writing the implementation of the class who is a member of the other class. Static data member: A static data member is underlined. nextID is static. To make it static just add ‘static’ in front in the type and we make it public: static int nextID; Inheritance: here Salary, Commission and Hourly inherit from Payment Downloaded by: simphiwe73 | Distribution of this document is illegal S - The Marketplace to Buy and Sell your Study Material UML visibility (access specifier): A + on the left side of a function says it is a public member. – means it is private, # means it’s under the protected access modifier. Return type is at the end of a member, function or variable Diffirence between and “”: When you include an existing class you put it between and when you include a header file or class that you created then you put it in between “ “ e.g. #include QString #include "accounting.h" Static function: When you call a static function from a class you don’t need to create an object of the class 1st . e.g. Take the QInputDialog class. Downloaded by: simphiwe73 | Distribution of this document is illegal S - The Marketplace to Buy and Sell your Study Material To call a function from an object of the class you would code like this: QInputDialog object; t(); //here we go dot and then followed by the function With a static function you don’t have to make an instance of that object, you use the class name and the :: operator. QString userINput = QInputDialog::getText(); //here we don’t have to declare an object. Remember :: tells you which class the function getText() belongs to const after a function: double LoyaltyCard:: currentValue() const { //If we change any data members in here compiler will give us an error. Thus //const after a function insures that we don’t change any data members } Always write const after all functions that doesn’t change any members in the function. So all ‘get’ functions should be changed. Also toString functions also get const To use QString & QStringList you need to include them in Qt: #include "QString" #include "QStringList" foreach: General Syntax: foreach(typeOfElement element_ofListVar, List) { do_somethingWithEachElement; } Example: void displaySalaries(EmployeeList& el, QString type){ cout "Displaying " type " employees: " "n"; foreach(Employee e, el){ Downloaded by: simphiwe73 | Distribution of this document is illegal S - The Marketplace to Buy and Sell your Study Material if(Payment()-getType() == type){ cout Name() "t" ID() "t" Payment()-pay() "n"; } } cout endl; } Example: QStringList strList; strLd("aaa"); strLd("bbb"); strLd("ccc"); foreach(QString s, strList) { cout s endl; } General class implementation documentation: The only difference between a member function decleration/definition and implementation header is that in the header it get class name followed by :: in front of the function name. Also no semicolon after header Declaration: void setDetail(QString name, QString email, bool isManufacturer); Implementation: void Vendor::setDetail(QString name, QString email, bool isManufacturer) { } If a variable is constant then the const comes infront of the type: Employee(const Employee& e); Virtual functions: Only put virtual in front of the base class function. Also add const and = 0 to make it pure virtual function, but you don’t have to make it a pure virtual function. ‘= 0’ makes it a pure virtual function so it will get overridden. A pure virtual function does not have implementation, so don’t implement it in .cpp file Downloaded by: simphiwe73 | Distribution of this document is illegal S - The Marketplace to Buy and Sell your Study Material class Payment { public: //virtual function in base class so add 'virtual' //keyword infront of base class function virtual double pay() const = 0; Now in the inheriting class just implement the functions normally and it will override the base class’ virtual function class Hourly : public Payment{ public: Hourly(double hr); void addHours(double hrs); double pay() const; in .cpp file double Hourly::pay() const{ return hours * hourlyRate; } Non pure virtual function: class A { public: virtual void display() //non pure virtual function { cout "display from class B" endl; } }; class B: public A { public: void display() { cout "display from class B" endl; } }; int main() { A *APtr; APtr = new B; APtr-display(); //will still override A's display function } Static implementations: Put static in front of variable when you declare it. static int nextID; Then @ the top of the class .cpp file add Downloaded by: simphiwe73 | Distribution of this document is illegal S - The Marketplace to Buy and Sell your Study Material type class_name::var_name = value; int Employee::nextID = 1001; Then increment the variable in constructor and decrement it in destructor Employee::Employee(QString fn, QString sn) { nextID++; } Copy constructor and assignment operator: Give the parameter a constant since we will not change the paramter variable itself. Also both of them will have the same parameter //copy constructor Employee::Employee(const Employee& e) { } How to implement constructor of inheriting class and also constant value SYNTAX: Classname::Classname(par_list):baseClassConstructor(arguments), data_mem1(par1) ,data_mem2(par2) Payment::Payment(QString typ):type(typ) { } Salary::Salary(double sal):Payment("Salary"), salary(sal) { } So what is happening here is when a salary is created then the salary’s constructor is invoked. It sends Salary to Payment constructor which then update the type data member of Payment Note we give payment a constant value, sometimes we need to give values constant values if a value is not provided in the parameter list. Look out for when they say a data member of base class is protected to allow it to be initialized by constructors of base classes Downloaded by: simphiwe73 | Distribution of this document is illegal S - The Marketplace to Buy and Sell your Study Material Increment by something: totalSales += sv; If you have a class that has another class as a data member and this class is a base class: Then you need to include all the inheriting classes in the top class(class that has other class as its data member) so Copy constructor and assignment operator with class pointers: So if we have this: Note that the class Employees are responsible for a creation of Employee objects since it has a constructor that will create Employees Employee e1("John","Smith"); //creating an employee object Note that we have a class pointer in Employee and it points to an Payment object. But this Payment class is an abstract class so no object will be made of it, so we are going to only make objects of the inheriting classes. So now in the copy constructor and assignment operator we will have a pointer of type base class which will point to objects of the inheriting class. So how do we do this: Employee::Employee(const Employee& e) { Downloaded by: simphiwe73 | Distribution of this document is illegal S - The Marketplace to Buy and Sell your Study Material if (Payment()-getType() == "salary") payment = new Salary((Salary&)*(nt)); else if (Payment()-getType() == "hourly") payment = new Hourly((Hourly&)*(nt)); else if (Payment()-getType() == "commission") payment = new Commission((Commission&)*(nt)); else payment = NULL; So in payment = new Salary((Salary&)*(nt)); the payment pointer is of type Payment pointer(so it must point to Payment), but we make it point to an inheriting class object. So we create a Salary object by going new Salary. Then in the brackets we must give this object the value of the parameter’s (e) pointer pointer (with the same name) so we do *(nt), but since this pointer is pointing to a inheriting class object we must do a conversion so we write in bracket in front of this value className&, which we write (Salary&) Explaining the Payment()-getType() //so e is an Employee that will be passed as argument. Then we call e's getPayment() function which returns e's payment pointer which is a pointer which points to a Payment object so now we have a payment object. Then we call this payment object's getType() function which then returns the type Polymorphism explained in the employee question: also called dynamic binding Polymorphism occurs when a virtual member function that is implemented and overridden in a hierarchy of classes, and this function is called via a superclass pointer. The actual version that is executed depends on the type of the object that the pointer is pointing to. The decision of which version to be executed is made at runtime. E.g.: Payment* paymentX = new Hourly(50); cout employeeX.getPayment()-pay() endl endl; //so here we have a pointer of type Payment which is the base class and it is pointing to an object of the inheriting class, Hourly. Both contain the function pay(). But since the object is of type Hourly when we call the pay function using the pointer then Hourly's pay function will get executed In the employee example: Note that for an Hourly employee we must add hours using the addHours function. But how do we do it since the function is in an inheriting function of Payment and Payment is a pointer class variable of Employee. Notice that Employee have a pointer that points to Payment. So what we do is we create a pointer that points to the inheriting class(Hourly) then we assign it(or let it point to) to the specific employee’s payment by using the employee’s getPayment() function. But we do a conversion of this payment to the class’s type by going className* in front of the Downloaded by: simphiwe73 | Distribution of this document is illegal S - The Marketplace to Buy and Sell your Study Material payment. Then we use this created pointer to call the function in the inheriting class Payment* paymentX = new Hourly(50); Employee employeeX("Hunter","MacaVoi"); employeeX.setPayment(paymentX); //add hours to employeeX //need to use Hourly's addHours function Hourly* hour = (Hourly*)employeeX.getPayment(); //Payment will return the payment pointer in Employee, the new let hour also point to what employeeX’s payment is pointing towards hour-addHours(34); //use this pointer to call the function cout employeeX.getPayment()-pay() endl endl; Returning a class pointer: it is the same as returning a normal variable: //how to return a class pointer Payment* Employee::getPayment() const { return payment; //payment points to Payment object } Partial UML diagram with QList: Note EmployeeList is not responsible for creating Employee objects. Employee class is So QListEmployee is a list of Employees. We have already created the Employee class and QList is a predefined class so we do not have write code for the QListEmployee class. We can simply make it(just include QList to make a QList). Employee has composite relationship with EmployeeList, because EmployeeList contain Employees So we can simply let EmployList class inherit from QListEmployee #include QList #include "employee.h" Downloaded by: simphiwe73 | Distribution of this document is illegal S - The Marketplace to Buy and Sell your Study Material class EmployeeList: public QListEmployee { } Note in the diagram that Employee is a part of EmployList class and it is, since Employees are the elements in the list of employees. So we don’t have to declare a variable of Employees How to create objects of the classes in main of the Employee program (look at UML diagram): So the main class is Employee because it has a class member variable and also we create employees(the program is about employees). Now this class have a pointer class member in this class (payment). So we must create Payment pointers and let it point to objects of the inheriting class( since Payment is abstract class which is not gona have pointers) Note if we just give a value in the parenthesis we don’t have to do conversion Payment * p1 = new Salary(12500.00); Payment * p2 = new Salary(25000.00); Payment * p3 = new Hourly(25.00); Now create actual employee objects. And then there is a function to set the employee’s to the correct payment method or inheriting class as to speak Employee e1("Ann", "Mentz"); Payment(p1); Employee e2("Ben", "Mathe"); Payment(p2); Just to show the setPayment function. void Employee::setPayment(Payment *pay) { payment = pay; } So what is happening we send the Payment pointer p1 to the setPayment function and the function let e1’s payment pointer to point to what p1 is pointing towards. And p1 is pointing to a Salary object. Lastly we append employees to the employee list.

Meer zien Lees minder
Instelling
University Of South Africa
Vak
COS2614 - Programming: Contemporary Concepts (COS2614)











Oeps! We kunnen je document nu niet laden. Probeer het nog eens of neem contact op met support.

Geschreven voor

Instelling
University of South Africa
Vak
COS2614 - Programming: Contemporary Concepts (COS2614)

Documentinformatie

Geüpload op
6 oktober 2021
Aantal pagina's
92
Geschreven in
2021/2022
Type
SAMENVATTING

Onderwerpen

$4.49
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kun je een ander document kiezen. Je kunt het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper

Seller avatar
De reputatie van een verkoper is gebaseerd op het aantal documenten dat iemand tegen betaling verkocht heeft en de beoordelingen die voor die items ontvangen zijn. Er zijn drie niveau’s te onderscheiden: brons, zilver en goud. Hoe beter de reputatie, hoe meer de kwaliteit van zijn of haar werk te vertrouwen is.
ExcelAcademia2026 Chamberlain College Of Nursing
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
2233
Lid sinds
4 jaar
Aantal volgers
1651
Documenten
9074
Laatst verkocht
1 dag geleden
EXCEL ACADEMIA TUTORS

At Excel Academia Tutoring, You will get solutions to all subjects in both assignments and major exams. Contact me for assistance. Good luck! Well-researched education materials for you. Expert in Nursing, Mathematics, Psychology, Biology etc. My Work has the Latest & Updated Exam Solutions, Study Guides and Notes (100% Verified Solutions that Guarantee Success)

3.7

377 beoordelingen

5
156
4
80
3
70
2
23
1
48

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Bezig met je bronvermelding?

Maak nauwkeurige citaten in APA, MLA en Harvard met onze gratis bronnengenerator.

Bezig met je bronvermelding?

Veelgestelde vragen