CSC 230 - Lecture 02
CSC 230 - Lecture 02
CSC230
array:
• User defines
• Combine multiple data items of same type
structure:
• User defines
• Combine multiple data items of different types
include <iostream>
#include <string>
using namespace std;
struct TCNJstudent
{
char name[50];
char major[50];
char homeAddress[100];
int id;
};
TCNJstudent immStudent;
struct TCNJstudent
{
char name[50];
char major[50];
char homeAddress[100];
int id;
};
infoCheck(csStudent);
Pointers to structures
5
struct TCNJstudent
{
char name[50];
char major[50];
char homeAddress[100];
int id;
};
infoCheck(&csStudent);
Class and Object
6
private:
};
Class and object example
7
#include <iostream>
#include <string>
using namespace std;
class student
{
public:
char name[50];
char major[50];
char homeAddress[100];
};
int main ()
{
student csStudent, mathStudent;
strcpy(csStudent.name, "Mike Lee");
strcpy(csStudent.major, "CS");
strcpy(csStudent.homeAddress, "Earth");
cout << csStudent.name << " " << csStudent.homeAddress <<endl;
return 0;
}
Method definition
8
scope operator
✔ ✔
Static vs. Non-static
9
class employee
{
public: count
...
int id;
static int counter; s1 s2
int getID(){ … …
return id; id id
}
}; …
s3 id
Const method/member function
10
class student
{
private :
function declaration
string name, addr, major;
public :
void info() const;
function definition
};
class TCNJstudent
{
private:
char name[50];
char major[50];
int id;
public:
void setName();
void setMajor();
TCNJstudent(); // constructor
void info() const;
};
Class Interface
13
TCNJstudent class
setName
Private data:
setMajor
name
major
TCNJstudent id
info
Access specifier
14
void circle::display(void)
{
std::cout << "r = " << radius << std::endl;
}
int main(void) {
circle c; // an object of circle class
c.store(5.0);
std::cout << "The area of circle c is " << c.area() << std::endl;
c.display();
}
Look inside the example
16
int main(void) {
circle c; // an object of circle class
c.store(5.0);
std::cout << "The area of circle c is " << c.area() << std::endl;
c.display();
}
int main(void) {
circle c, *d;
d.store(5.0);
std::cout << "The area of circle c is " << d.area() << std::endl;
d.display();
}
Does this one work?
18
int main(void) {
circle c, *d;
d.store(5.0);
std::cout << "The area of circle c is " << d.area() << std::endl;
d.display();
}
int main(void) {
circle c, *d;
d = &c;
d.store(5.0);
std::cout << "The area of circle c is " << d.area() << std::endl;
d.display();
}
• d is initialized
• d is a pointer, we cannot use d.store(), d.area(), d.display() to access the functions.
Second modification
20
int main(void) {
circle c, *d;
d = &c;
d->store(5.0);
std::cout << "The area of circle c is " << d->area() << std::endl;
d->display();
}
✔
Pointer, dynamic memory
21
d is dynamically allocated
int main(void) {
circle *d;
d = new circle();
d->store(5.0);
std::cout << "The area of circle c is " << d->area() << std::endl;
d->display();
}
More example of dynamic memory
22
int main(void) {
circle *d;
d = new circle();
int * e = new int[15];
d->store(5.0);
std::cout << "The area of circle c is " << d->area() << std::endl;
d->display();
delete d;
delete[] e;
}
int main(void) {
circle *d;
d = new circle();
d->set(5.0);
circle c;
c.set(4.0);
}
Object initialization, Constructor
24
class circle
{
private:
double radius;
class circle
When a class is declared with no
{ constructors,
private: the compiler automatically assumes default
double radius; constructor and copy constructor for it.
• Default constructor
public:
void set(double r);
}; circle:: circle() { };
• Copy constructor
public:
void set(double r);
}; • Initialize with copy constructor
class circle
{
If any constructor is declared,
public: • no default constructor will exist,
double radius; unless you define it.
• still have copy constructor
public:
✗
void set(double r);
circle r1;
circle(double r){radius = r;}
};
• Initialize with constructor
circle r1(5.0);
circle *r2 = new circle(6.0);
✔
Constructor and destructor
28
account::~account()
{
Destructor definition
delete[] name;
} Delete whole string.
A .cpp file containing the main() function should include all the corresponding .h
files where the functions used in .cpp file are declared.
Example: TCNJstudent.h
31
class TCNJstudent
{
private:
char name[50];
char major[50];
int id;
public:
void setName();
void setMajor();
TCNJstudent();
void info() const;
};
Example: TCNJstudent.cpp
32
#include <iostream>
Assume the implementation needs this file
#include <string>
#include "TCNJstudent.h"
using namespace std; Must include the corresponding
header file
void TCNJstudent::setName()
{…
} To simplify the example, we use blank body.
void TCNJstudent::setMajor() A real implementation can have various
{… body.
}
TCNJstudent::TCNJstudent()
{…
}
void TCNJstudent::info()
{…
}
Example: main.cpp
33
#include "TCNJstudent.h"
Must include the corresponding header file
int main(){
…
}
Compile
g++ -o excuFile main.cpp TCNJstudent.cpp
specification file
main program TCNJstudent.h implementation file
main.cpp TCNJstudent.cpp
#include “TCNJstudent.h”
Compiler Compiler
main.o TCNJstudent.o
Linker
execFile
Inheritance
35
Polygon
class Polygon{
private:
int numVertices;
float *xCoord, *yCoord;
public:
void set(float *x, float *y, int nV);
};
Triangle
Syntax:
class derived-class : access-specifier base-class
where
access-specifier is one of public, protected, or private
private by default
Most of the time, people use public
class Point{
point protected:
int x, y;
public:
void set (int a, int b);
};
circle
class circle : public point{
private:
double r;
cylinder ... ...
};
Base
class/superc How to decide the access specifiers of the
lass/parent members in the subclass?
class
• Class definition
Members go to
• Inheritance type
Derive from
class Polygon{
protected:
int numVertices;
float *xCoord, *yCoord;
public:
Derived void set(float *x, float *y, int nV);
class/subclass/ };
child class
Type of Inheritance
of superclass member
Access specifier
class father{
private: int fPrv;
protected:
int getPrivateValue(){ A protected function access the private member
return fPrv;
}
};
✔
};
int main(){
son obj;
cout<<obj.foo()<<endl;
cout<<obj.fPrv<<endl;
}
What is inherited?
44
• Some exceptions:
• Constructor and destructor
• Operator=() member
• Friends
Without explicit specification, the default constructor and destructor of the base class
will be called first when a new object of the derived class is created or destroyed.
class A{
public:
A(){
cout<< "A: default constructor"<<endl; When B(int a)is executed, A() will
} be executed first.
A(int a){
cout<<"A: with a"<<endl;
}
} If there is a statement in the main():
The constructor and destructor of the derived class can specify which
constructor/destructor should be invoked.
class A{
public:
A(){
cout<< "A: default constructor"<<endl;
}
A(int a){
cout<<"A: with a"<<endl;
}
} If there is a statement in the main():
class A {
protected:
int x, y; Method in parent class
public:
void print ()
{cout<<"From A"<<endl;}
};
class B : public A {
public: Method in child class with same signature
void print ()
{cout<<"From B"<<endl;}
};
Overriding
49
class A {
protected:
int x, y;
public:
void print ()
{cout<<"From A"<<endl;}
};
class B : public A {
public:
void print ()
{ Call the print() in A class
A::print(); NO super keyword in C++
cout<<"From B"<<endl;
}
};
this pointer in C++
50
Given a object, each member function of this object has a implicit parameter of this, only
member function has this. Friend functions don’t have it.
A review of inheritance
51
#include <string>
using namespace std;
class TCNJstudent
{
private:
string name;
string major;
int id;
public:
void setName(string a);
void setMajor(string a);
TCNJstudent();
void info() const;
};
TCNJstudent.cpp
53
#include <iostream>
#include <string> void TCNJstudent::info()
#include "TCNJstudent.h" {…
using namespace std;
}
void TCNJstudent::setName(string a)
{…
}
void TCNJstudent::setMajor(string a)
{
name = a;
}
TCNJstudent::TCNJstudent()
{…
}
CSstudent.h
54
Parent class
#include "TCNJstudent.h" declaration
#include <string>
using namespace std;
#include <iostream>
#include "CSstudent.h"
void CSstudent::setMajor(){
TCNJstudent::setMajor("CS");
}
CSstudent::CSstudent(){
cout <<"From CSstudent()"<<endl;
}
#include "CSstudent.h"
int main(){
CSstudent stu;
stu.setMajor();
CSstudent stu2("Mike", "CS", "NJ");
}
Compile
g++ -o excuFile main.cpp TCNJstudent.cpp CSstudent.cpp
Class Interface
57
setName setName
Private data:
setMajor setMajor
name
id
CSstudent info
info
Polymorphism
58
Polymorphism:
• Many forms
• Usually used in inheritance
• Call a function, which has different implementations
Polymorphism
59
By default, C++ checks the type of the variable (poly), call the function in the
corresponding type. This is called static resolution/linage.
If we want C++ to check the contents of the pointer instead of it’s type. We can add
“virtual” to the function in the base class.
class Polygon{
protected:
int numVertices;
float *xCoord, *yCoord;
public:
virtual void set(){
cout<<"From Polygon"<< endl;
}
};
Virtual Function
61
class Polygon{
protected:
int numVertices;
float *xCoord, *yCoord;
set() is a virtual
public:
function
virtual void set(){
cout<<"From Polygon"<< endl;
}
};
When we call a virtual function, such as set(), C++ uses dynamic linkage/late binding.
The selected function is based on the kind of the object.
After adding “virtual” to the previous program, the result will be:
From Rectangle
From Triangle
Pure Virtual Function
62
Any class with at least one pure virtual function is a abstract class (interface). Abstract
class provides an appropriate base class from which other classes can inherit.
• Abstract class cannot instantiate objects
• If a child class of an abstract class does not implement all pure virtual functions, itself
is an abstract class.
• If a child class of an abstract class does not have any pure virtual function, it is called
concrete class, which can instantiate objects.
Abstract class vs. concrete class
64
class Box{
Abstract class
✗
public:
virtual double getVolume()=0;
protected: Box obj;
double length;
double breadth;
double height;
};
✔
public:
double getVolume(){
return length*breadth*height; Rectangle obj;
}
};
✗
public:
void info(){}
}; Square obj;
Files and Streams
65
• iostream
• cin: standard input
• cout: standard output
• fstream
• ofstream: represents output file; is used to create files and to write
files
• ifstream: represents input file; is used to read files
• fstream: represents file stream generally; is a superset of ofstream
and ifstream
Open a file
66
ofstream object
ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );
fstream afile;
afile.open("file.dat", ios::out | ios::in );
eam>
tream> cout << "Enter your age: ";
ace std; cin >> data;
// write inputted data into the file.
outfile << data << endl;
ain ()
data[100];
en a file in read mode.
eam infile;
e.open("afile.dat");
data
(static)
heap (dynamic)
Code
Stack
Dynamic memory
72