Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C/C++ Cheat Sheet , Cheat Sheet of C programming

Useful overview on C and C++ Programming for the exam

Typology: Cheat Sheet

2019/2020

Uploaded on 10/09/2020

ashnay
ashnay 🇺🇸

4.8

(9)

238 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C/C++ Cheat Sheet
For your reference; this sheet will also be included in exams
CISC 124, fall 2004
Sample C++ Program:
#include <stdio.h>
#include <string.h>
class Employee {
private:
char name[51];
double wage; // hourly wage
protected:
double payOwed;
public:
Employee(char n[], double w) {
strcpy(name, n);
setWage(w);
payOwed = 0;
} // end constructor
virtual void pay(double hours) {
payOwed += wage * hours;
} // end pay
double amountToPay() {
return payOwed;
} // end amountToPay
void setWage(double wage) {
this->wage = wage;
}
void zero() {
payOwed = 0;
} // end zero
// prints employee
virtual void print() {
printf("name = %s, ", name);
printf("wage = $%.2f, ", wage);
printf("payOwed = $%.2f",
payOwed);
} // end print
void println() {
print();
printf("\n");
} // end println
}; // end class Employee
class Salesperson: public Employee {
private:
double rate;
public:
Salesperson(char name[],
double wage,
double rate)
: Employee(name, wage) {
this->rate = rate;
} // end constructor
void payForSale(double amount) {
payOwed += amount * rate;
} // end payForSale
void print() {
printf("Salesperson ");
Employee::print();
printf(", rate: $%.2f\n", rate);
} // end print
}; // end Salesperson
int main(void) {
Employee mickey("Mickey Mouse", 20);
mickey.setWage(21);
mickey.println();
Salesperson *donald =
new Salesperson("Donald Duck",
10, 0.15);
donald->payForSale(100);
donald->println();
Employee *company[2];
company[0] = &mickey;
company[1] = donald;
for (int i = 0; i < 2; i++) {
company[i]->pay(10);
company[i]->println();
} // end for
} // end main
pf2

Partial preview of the text

Download C/C++ Cheat Sheet and more Cheat Sheet C programming in PDF only on Docsity!

C/C++ Cheat Sheet

For your reference; this sheet will also be included in exams

CISC 124, fall 2004

Sample C++ Program: #include <stdio.h> #include <string.h> class Employee { private: char name[51]; double wage; // hourly wage protected: double payOwed; public: Employee(char n[], double w) { strcpy(name, n); setWage(w); payOwed = 0; } // end constructor

virtual void pay(double hours) { payOwed += wage * hours; } // end pay

double amountToPay() { return payOwed; } // end amountToPay

void setWage(double wage) { this->wage = wage; }

void zero() { payOwed = 0; } // end zero

// prints employee virtual void print() { printf("name = %s, ", name); printf("wage = $%.2f, ", wage); printf("payOwed = $%.2f", payOwed); } // end print

void println() { print(); printf("\n"); } // end println

}; // end class Employee

class Salesperson: public Employee { private: double rate; public: Salesperson(char name[], double wage, double rate) : Employee(name, wage) { this->rate = rate; } // end constructor

void payForSale(double amount) { payOwed += amount * rate; } // end payForSale

void print() { printf("Salesperson "); Employee::print(); printf(", rate: $%.2f\n", rate); } // end print

}; // end Salesperson

int main(void) { Employee mickey("Mickey Mouse", 20); mickey.setWage(21); mickey.println();

Salesperson *donald = new Salesperson("Donald Duck", 10, 0.15); donald->payForSale(100); donald->println(); Employee *company[2]; company[0] = &mickey; company[1] = donald; for (int i = 0; i < 2; i++) { company[i]->pay(10); company[i]->println(); } // end for } // end main

printf formats: %d: integer %f: float or double %s: string (char array) %c: char (single character)

scanf formats: %d: integer %f: float %lf: double (first character is L, not one!) %s: string (char array) %c: char (single character)

string methods: /* to use these methods, you must include <string.h> */ strcpy(char dest[], char src[]) copies src into dest int strlen(char s[]) returns length of s int strcmp(char s1[], char s2[]) returns negative if s1 < s2, 0 if s1 == s positive if s1 > s strcat(char dest[], char src[]) adds src to the end of dest

abstract classes and methods: virtual void sound(char s[]) = 0; // Reminder: no "abstract" keyword. // Class headers do not indicate // whether the class is abstract or // not. A class is abstract if it // contains any abstract methods.