Testing – Twitter Feed

This is to set the feed to twitter!

This is to set the feed to twitter!

This is to set the feed to twitter!

This is to set the feed to twitter!

This is to set the feed to twitter!

This is to set the feed to twitter!

Advertisements

Week 5 – Polymorphism

Polymorphism
-Adhoc (weak polymorphism / finite)
-coercion
-overloading
-Universal (strong polymorphism / infinite )
-inclusion
-parametric

Polymorphism
-same region in memory, used in many ways

void func(int x);
func(1); <—– int
func(10.5); <—– double
(transfer from one data type to another data type)

*coercion
-casting
-simplest form of polymorphism
-doesn't stay true to "polymorphic" character
-finite (limited) set of types

*overloading
-many functions with same name, different arguments
-also weak polymorphism character, since code paths not related.
-limited / finite set of types

*inclusion
-inheritance
-Dynamic
-object type detected at runtime
-potentially infinite reuse of code, as long as required base class is extended.
A — root
——————-
B
C
|
D
E
F

class A{
public:
//complier knows when to call it.
const char* getName(){ return "A";}
};
class B:public A{
const char* getName(){ return "B";}
};

cout << b.getName(); //B
A* a = &b;
cout <getName(); //A

sizeof(A) –> 0

class A{
public:
//by adding virtual function is called at run-time
virtual const char* getName(){ return “A”;}
};
class B:public A{
const char* getName(){ return “B”;}
};

cout << b.getName(); //B
A* a = &b;
cout <
getName(); //A

sizeof(A) –> 4 (pointer are 4 bytes)

*Parametric

-Different from inclusion in:
-no base class requirement
-no virtual
-compiled time
-Templated code
-logic is uniform across all types
-requirements not laid out up-front
-compiler generates code, on-demand, on best effort basis

Function template
template —> or type name
T adder(T a, T b){
return a + b;
}
cout << adder(5, 6) << endl; //11 (compiler figures out it's int function)
cout << adder(3.5, 7.9) << endl; //11.4 (compiler figures out it's double function)

cout << adder(3.5, 6) << endl; //compiler crashes (can't figure out int and double)
cout << adder(3.5, 6) << endl; //quick fix to make it INT output.

template //3 different class
C func(const A& a, const B& b, const C& c){ //C must have a copy constructor
a.performOp(B); //A must have const function performOp that can accept const B&
return a.computation(); //A must have const function computation that returns type convertible to C
}

template //any integral type eg. int, long, short, char signed or unsigned.
void func(T default, T increment){
T _array[N];
for(short i = 0; i 0; i–){
cout << _array[i-1];
}
}

func(1,3);
func(6,2);

****Synonyms******
typename = class inside a template

Week 5 – Assignment 1 discussed again

Assignment discussed

-left/right

Left:
Can I move cursor left?
Do that, leave
Otherwise, can I shift string right?
Do that, leave
Otherwise,
Leave.

Right:
Similar, but taking into account
-string length
-screen width
-field width

console.cols() — number of columns
col — starting column
fieldlen — given field length

if((col + fieldlen) > console.cols()){
fieldlen = console.cols() – col;
}

#define _CRT_SECURE_NO_WARNINGS

Week 4

Storage Duration
-extern : declares area of memory (variable) to be shared by whole program

-static : -inside function body, static variable value for duration of program
-global space, available only in that cpp file
-inside class definition, all instances of class gain access & duration of var is program

-polymorphism
-inheritance
-encapsulation

register x = 5;
-assumes x is integer
-places x in cpu register

volatile int x = 5;
-complier can’t make assumptions about this data.
-always have to always read / write data cant optimize.

void main(){
char c;
int i;
char* c_p = &c;
int* i_p = &i;
c_p = (char*)i_p;

Pointers (4bytes on 32 bit platform)
void*
void* x;
void func(void* v);
void* func2();

-a void pointer can be assigned without a cast.
c_p = i_p;
i_p = (int*)c_p;

void main(){

char* m = “hello”;

cout << m[2]; //output: l

void* v = m;
cout < int*

const int i = 7; legal
i = 8; illegal
const int* ip = &i; legal
*ip = 9; illegal
int j = *ip; legal
int* ip2 = (int*)ip; legal
*ip2 = 12; legal

Week 3 – Assignment 1 discussed

Learned Visual Studio 2012

consolelineedit.cpp
#include
#include “console.h”
#include “consoleplus.h”

namespace cio{
void display(const char *str, int row, int col, int fieldLen){

int numCharsToPrint;

if(!str){
numCharsToPrint = 0;
}
else{
int strLen = std::strlen(str);
if(!fieldLen){
numCharsToPrint = strlen;
}
else{
numCharsToPrint = fieldLen;
if(strLen < fieldLen) numCharsToPrint = strLen;
}
}

for(int i = 0; i < numCharsToPrint; i++){
console.setPosition(row, col + i);
console.setCharacter(str[i]);
console.setPosition(row, col + i);
console.drawCharacter();
}

}

int edit(char *str, int row, int col, int fieldLength, int maxStrLength, bool* insertMode, int* strOffset, int* curPosition){

return 0;
}
}

there’s still bug in it

Week 3 – Namespaces

Namespaces

-Organizational constructs

using namespace std;
std::cout <<
but instead you can use this
cout <<

using namespace cat;
using namespace dog;

void main(){
std::cout << getName() << endl; (error, because both cat and dog has getName() )

*namespace Dog{
namespace More{
const char* getName();
}
}

void main(){
std::cout << More::getName() << endl;
}

-using namespace cat;
using namespace dog::More;

void main(){
std::cout << getName() <namespace{
const char* other(){
return “other”;
}
}

-my.cpp is translation unit, and anything declared above is constrained to this particular translation unit.

Namespace cat{
Const char* getName(){
Return “cat”;
}
}

Namespace Dog{
Const char* getName(){
Return “dog”;
}
}

Cat::getName

Dog::getName

animal.h
namespace cat{
const char* getName();
}

namespace dog{
const char* getName();
}

animal.cpp
#include “animal.h”

const char* cat::getName(){

}

namespace cat{
const char* getName(){
return “cat”;
}
}

OOP344 quiz 1 answer discussed in class

Quiz 1 discussed in class.  Here’s the solution.  However, it’s a little bit different coding style to what I am used to.

Badger::Badger(){
_storage = NULL;
}

//Constructor
Badger::Badger(const char* src){
_storage = 0;
set(c);
}

Badger::Badger(const Badger& src){
_storage = 0;
//deep copy
set(src._storage);
}

//copy constructor
void Badger::set(const char* src){
if(_storage){
delete [] _storage;
_storage = NULL;
}
if(src){
_storage = new char[strlen(c) + 1];
strcpy(_storage, src);
}
}

//Destructor
Badger::~Badger(){
if(_storage){
delete [] _storage;
}
}

//display function
void Badger::print() const{
if(_storage){
std::cout << _storage;
}
}

int Badger::length() const{
if(_storage){
return strlen(_storage);
}
else{
return 0;
}
}