repo update
This commit is contained in:
@@ -1,25 +1,25 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
/*
|
||||
void swap(int x, int y) {
|
||||
int temp = x;
|
||||
x = y;
|
||||
y = temp;
|
||||
}
|
||||
*/
|
||||
|
||||
void swap(int* x, int* y) {
|
||||
int temp = *x;
|
||||
*x = *y;
|
||||
*y = temp;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int A = 2;
|
||||
int B = 8;
|
||||
// Pass in addresses to be
|
||||
swap(&A,&B);
|
||||
cout << "A: " << A << " B: " << B << endl;
|
||||
|
||||
return 0;
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
/*
|
||||
void swap(int x, int y) {
|
||||
int temp = x;
|
||||
x = y;
|
||||
y = temp;
|
||||
}
|
||||
*/
|
||||
|
||||
void swap(int* x, int* y) {
|
||||
int temp = *x;
|
||||
*x = *y;
|
||||
*y = temp;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int A = 2;
|
||||
int B = 8;
|
||||
// Pass in addresses to be
|
||||
swap(&A,&B);
|
||||
cout << "A: " << A << " B: " << B << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,23 +1,23 @@
|
||||
#makefile comment
|
||||
|
||||
CC = g++
|
||||
CFLAGS = -C
|
||||
LFLAGS =
|
||||
SOURCES = math.cpp main.cpp
|
||||
OBJECTS = $(SOURCES:.cpp=.o)
|
||||
EXECUTABLE = making_ex1
|
||||
|
||||
all: $(EXECUTABLE) $(SOURCES)
|
||||
|
||||
$(EXECUTABLE): $(OBJECTS)
|
||||
$(CC) $(LFLAGS) -o $(EXECUTABLE) $(OBJECTS)
|
||||
|
||||
%.o:%.cpp
|
||||
$(CC) $(CFLAGS) $<
|
||||
|
||||
-include
|
||||
|
||||
clean:
|
||||
rm -f *.o
|
||||
rm -f $(EXECUTABLE)
|
||||
#makefile comment
|
||||
|
||||
CC = g++
|
||||
CFLAGS = -C
|
||||
LFLAGS =
|
||||
SOURCES = math.cpp main.cpp
|
||||
OBJECTS = $(SOURCES:.cpp=.o)
|
||||
EXECUTABLE = making_ex1
|
||||
|
||||
all: $(EXECUTABLE) $(SOURCES)
|
||||
|
||||
$(EXECUTABLE): $(OBJECTS)
|
||||
$(CC) $(LFLAGS) -o $(EXECUTABLE) $(OBJECTS)
|
||||
|
||||
%.o:%.cpp
|
||||
$(CC) $(CFLAGS) $<
|
||||
|
||||
-include
|
||||
|
||||
clean:
|
||||
rm -f *.o
|
||||
rm -f $(EXECUTABLE)
|
||||
rm -f *.d
|
||||
@@ -1,71 +1,71 @@
|
||||
#include <iostream>
|
||||
#include "dog.h"
|
||||
using namespace std;
|
||||
|
||||
Dog::Dog(int age, int namelength, char * name) {
|
||||
this->age = age;
|
||||
this->namelength = namelength;
|
||||
this->name = new char[namelength];
|
||||
for(int i = 0; i < namelength; i++){
|
||||
this->name[i] = name[i];
|
||||
}
|
||||
}
|
||||
|
||||
Dog::~Dog(){
|
||||
if(this->namelength > 0){
|
||||
delete[] this->name;
|
||||
}
|
||||
}
|
||||
|
||||
Dog::Dog(const Dog& from){
|
||||
this->age = from.age;
|
||||
this->namelength = from.namelength;
|
||||
this->name = new char[this->namelength];
|
||||
for(int i = 0; i< this->namelength; i++){
|
||||
this->name[i] = from.name[i];
|
||||
}
|
||||
}
|
||||
|
||||
Dog& Dog::operator=(const Dog& rhs){
|
||||
if(&rhs != this){
|
||||
// delete existing memory
|
||||
if(namelength > 0){
|
||||
delete[] this->name;
|
||||
}
|
||||
this->age = rhs.age;
|
||||
this->namelength = rhs.namelength;
|
||||
this->name = new char[namelength];
|
||||
for(int i = 0; i < namelength; i++){
|
||||
this->name[i] = rhs.name[i];
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
char Dog::operator[](int index) const{
|
||||
if(index >= namelength){
|
||||
// Use with try/catch when called in main do exception e.what()
|
||||
throw(out_of_range("index is larger than name length"));
|
||||
}
|
||||
return name[index];
|
||||
}
|
||||
|
||||
char& Dog::operator[](int index){
|
||||
if(index >= namelength){
|
||||
// Use with try/catch when called in main do exception e.what()
|
||||
throw(out_of_range("index is larger than name length"));
|
||||
}
|
||||
return name[index];
|
||||
}
|
||||
|
||||
Dog Dog::puppy(int namelength, char * name){
|
||||
Dog p(0, namelength, name);
|
||||
return p;
|
||||
}
|
||||
|
||||
ostream& operator<<(ostream& os, const Dog& d){
|
||||
for(int i = 0; i< d.namelength; i++) {
|
||||
os << d[i];
|
||||
}
|
||||
return os;
|
||||
#include <iostream>
|
||||
#include "dog.h"
|
||||
using namespace std;
|
||||
|
||||
Dog::Dog(int age, int namelength, char * name) {
|
||||
this->age = age;
|
||||
this->namelength = namelength;
|
||||
this->name = new char[namelength];
|
||||
for(int i = 0; i < namelength; i++){
|
||||
this->name[i] = name[i];
|
||||
}
|
||||
}
|
||||
|
||||
Dog::~Dog(){
|
||||
if(this->namelength > 0){
|
||||
delete[] this->name;
|
||||
}
|
||||
}
|
||||
|
||||
Dog::Dog(const Dog& from){
|
||||
this->age = from.age;
|
||||
this->namelength = from.namelength;
|
||||
this->name = new char[this->namelength];
|
||||
for(int i = 0; i< this->namelength; i++){
|
||||
this->name[i] = from.name[i];
|
||||
}
|
||||
}
|
||||
|
||||
Dog& Dog::operator=(const Dog& rhs){
|
||||
if(&rhs != this){
|
||||
// delete existing memory
|
||||
if(namelength > 0){
|
||||
delete[] this->name;
|
||||
}
|
||||
this->age = rhs.age;
|
||||
this->namelength = rhs.namelength;
|
||||
this->name = new char[namelength];
|
||||
for(int i = 0; i < namelength; i++){
|
||||
this->name[i] = rhs.name[i];
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
char Dog::operator[](int index) const{
|
||||
if(index >= namelength){
|
||||
// Use with try/catch when called in main do exception e.what()
|
||||
throw(out_of_range("index is larger than name length"));
|
||||
}
|
||||
return name[index];
|
||||
}
|
||||
|
||||
char& Dog::operator[](int index){
|
||||
if(index >= namelength){
|
||||
// Use with try/catch when called in main do exception e.what()
|
||||
throw(out_of_range("index is larger than name length"));
|
||||
}
|
||||
return name[index];
|
||||
}
|
||||
|
||||
Dog Dog::puppy(int namelength, char * name){
|
||||
Dog p(0, namelength, name);
|
||||
return p;
|
||||
}
|
||||
|
||||
ostream& operator<<(ostream& os, const Dog& d){
|
||||
for(int i = 0; i< d.namelength; i++) {
|
||||
os << d[i];
|
||||
}
|
||||
return os;
|
||||
}
|
||||
@@ -1,18 +1,18 @@
|
||||
using namespace std;
|
||||
|
||||
class Dog {
|
||||
public:
|
||||
Dog(int age, int namelength, char * name);
|
||||
// has to be dog object, not reference
|
||||
static Dog Dog::puppy(int namelength, char *name);
|
||||
Dog(const Dog& from);
|
||||
~Dog();
|
||||
Dog& operator=(const Dog& rhs);
|
||||
char operator[](int index) const;
|
||||
char& operator[](int index);
|
||||
friend ostream& operator<<(ostream& os, const Dog& d);
|
||||
private:
|
||||
int age;
|
||||
int namelength;
|
||||
char * name;
|
||||
using namespace std;
|
||||
|
||||
class Dog {
|
||||
public:
|
||||
Dog(int age, int namelength, char * name);
|
||||
// has to be dog object, not reference
|
||||
static Dog Dog::puppy(int namelength, char *name);
|
||||
Dog(const Dog& from);
|
||||
~Dog();
|
||||
Dog& operator=(const Dog& rhs);
|
||||
char operator[](int index) const;
|
||||
char& operator[](int index);
|
||||
friend ostream& operator<<(ostream& os, const Dog& d);
|
||||
private:
|
||||
int age;
|
||||
int namelength;
|
||||
char * name;
|
||||
};
|
||||
@@ -1,16 +1,16 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int & multiply(int A, int B) {
|
||||
int i = A*B;
|
||||
return i;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int A = 5;
|
||||
int B = 3;
|
||||
int C = multiply(A,B);
|
||||
cout << "A: " << A << " B: " << B << " C: " << C << endl;
|
||||
|
||||
return 0;
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int & multiply(int A, int B) {
|
||||
int i = A*B;
|
||||
return i;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int A = 5;
|
||||
int B = 3;
|
||||
int C = multiply(A,B);
|
||||
cout << "A: " << A << " B: " << B << " C: " << C << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
// int length;
|
||||
// cin >> length;
|
||||
int array1[3] = {3, 10, 40};
|
||||
cout << array1 << endl;
|
||||
cout << *array1 << endl;
|
||||
cout << array1 << endl;
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
// int length;
|
||||
// cin >> length;
|
||||
int array1[3] = {3, 10, 40};
|
||||
cout << array1 << endl;
|
||||
cout << *array1 << endl;
|
||||
cout << array1 << endl;
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
27
Lecture/Week5/Lab5.txt
Normal file
27
Lecture/Week5/Lab5.txt
Normal file
@@ -0,0 +1,27 @@
|
||||
Shape class
|
||||
data:
|
||||
color (required)
|
||||
1st point
|
||||
# points
|
||||
functions:
|
||||
constructor(color)? <- optional
|
||||
shape::shape(...) <- do this in child?
|
||||
copy constructor
|
||||
assignment operator
|
||||
destructor (virtual)
|
||||
draw <- pure virtual
|
||||
Shape::draw(Graphics context gc)
|
||||
gc->setColor(color)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Shape * S1 = new Line(...)
|
||||
Shape * S2 = new Triangle(...)
|
||||
|
||||
S1->draw(...) draw Line
|
||||
S2->draw(...) draw Triangle
|
||||
|
||||
|
||||
Image class
|
||||
28
Lecture/Week5/main.cpp
Normal file
28
Lecture/Week5/main.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Pet{
|
||||
public:
|
||||
string name;
|
||||
void set_data(int data){}
|
||||
};
|
||||
|
||||
class Cat: public Pet{
|
||||
public:
|
||||
int fish;
|
||||
void set_data(int data){fish = data;}
|
||||
};
|
||||
|
||||
class Bird: public Pet{
|
||||
public:
|
||||
int seed;
|
||||
void set_data(int data){seed = data;}
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
Pet* C1 = new Cat;
|
||||
Pet* C2 = new Cat;
|
||||
C1->name = "Cat1";
|
||||
C1->set_data(1);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user