Skip to content

Instantly share code, notes, and snippets.

@ProfAvery
Last active October 22, 2024 17:25
Show Gist options
  • Save ProfAvery/6aa32efb2eb794f2f26a0011ea7249f5 to your computer and use it in GitHub Desktop.
Save ProfAvery/6aa32efb2eb794f2f26a0011ea7249f5 to your computer and use it in GitHub Desktop.
CPSC 458 - C++ Code from Chapter 20
#include <iostream>
#include <tchar.h>
class SimpleClass
{
public:
int x;
void HelloWorld()
{
printf("Hello World\n");
}
};
int _tmain(int argc, _TCHAR *argv[])
{
SimpleClass myObject;
myObject.HelloWorld();
}
#include <iostream>
#include <tchar.h>
class SimpleClass
{
public:
int x;
void HelloWorld()
{
if (x == 10)
printf("X is 10.\n");
}
};
int _tmain(int argc, _TCHAR *argv[])
{
SimpleClass myObject;
myObject.x = 9;
myObject.HelloWorld();
SimpleClass myOtherObject;
myOtherObject.x = 10;
myOtherObject.HelloWorld();
}
#define GENERIC_READ 1
class String
{
// ...
public:
String(char *);
};
void LoadFile(String filename)
{
// ...
}
void LoadFile(String filename, int options)
{
// ...
}
void Main()
{
LoadFile("c:\\myfile.txt");
LoadFile("c:\\myfile.txt", GENERIC_READ);
}
class INetAddr
{
// ...
};
class Socket
{
// ...
public:
void setDestinationAddr(INetAddr *addr)
{
// ...
}
// ...
};
class UDPSocket : public Socket
{
public:
void sendData(char *buf, INetAddr *addr)
{
setDestinationAddr(addr);
// ...
}
// ...
};
int main()
{
UDPSocket sock;
INetAddr *addr = new INetAddr;
sock.sendData("Hello World", addr);
}
# Note: to compile for 64-bit, open the "x64 Native Tools Command Prompt"
all: listing_20-1.obj listing_20-2.obj listing_20-4.obj listing_20-5.obj nonvirtual.obj virtual.obj
listing_20-1.obj: listing_20-1.cpp
cl /c listing_20-1.cpp
listing_20-2.obj: listing_20-2.cpp
cl /c listing_20-2.cpp
listing_20-4.obj: listing_20-4.cpp
cl /c listing_20-4.cpp
listing_20-5.obj: listing_20-5.cpp
cl /c listing_20-5.cpp
nonvirtual.obj: nonvirtual.cpp
cl /c nonvirtual.cpp
virtual.obj: virtual.cpp
cl /c virtual.cpp
clean:
del *.obj
#include <stdio.h>
#include <tchar.h>
class A
{
public:
void foo()
{
printf("Class A\n");
}
};
class B : public A
{
public:
void foo()
{
printf("Class B\n");
}
};
void g(A &arg)
{
arg.foo();
}
int _tmain(int argc, _TCHAR *argv[])
{
B b;
A a;
g(b);
return 0;
}
#include <stdio.h>
#include <tchar.h>
class A
{
public:
virtual void foo()
{
printf("Class A\n");
}
};
class B : public A
{
public:
virtual void foo()
{
printf("Class B\n");
}
};
void g(A &arg)
{
arg.foo();
}
int _tmain(int argc, _TCHAR *argv[])
{
B b;
A a;
g(b);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment