0% found this document useful (0 votes)
37 views

IEG 3080 Tutorial One: Course Outline C++ Basics Data Member & Member Function Constructor & Destructor

The document provides an overview of the topics to be covered in the IEG 3080 tutorial, including C++ basics, data members and member functions, constructors and destructors. Specifically, it will cover software development methodology, the C++ language features, programming assignments, and a final exam. It then discusses C++ file types, compilation, object-oriented concepts in C++, differences between classes and structs, class definition with declaration and implementation, use of private, public and protected access specifiers, constructors for initializing objects, and destructors for cleaning up objects.

Uploaded by

sudinavada2009
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

IEG 3080 Tutorial One: Course Outline C++ Basics Data Member & Member Function Constructor & Destructor

The document provides an overview of the topics to be covered in the IEG 3080 tutorial, including C++ basics, data members and member functions, constructors and destructors. Specifically, it will cover software development methodology, the C++ language features, programming assignments, and a final exam. It then discusses C++ file types, compilation, object-oriented concepts in C++, differences between classes and structs, class definition with declaration and implementation, use of private, public and protected access specifiers, constructors for initializing objects, and destructors for cleaning up objects.

Uploaded by

sudinavada2009
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

IEG 3080 Tutorial One

Course Outline
C++ Basics
Data member & Member function
Constructor & Destructor

Prepared by Brian Lam Tak Cheung 1


Course Outline
Software Development Methodology
DP (Design Pattern)
Steps/Models in Software Development Process
UML (Unified Modeling Language)
In short, develop software in an OO way

C++
Better C
e.g. cout vs printf(), vector vs array, const vs #define
e.g. new vs malloc(), inline vs #define, reference vs pointer
Generic Programming
e.g. template, container, iterator, algorithms
A good tool for OOP (Object-Oriented Programming)
Prepared by Brian Lam Tak Cheung 2
Assessment Scheme

Programming assignments 15%


Approximately 3 assignments with phases

Group project 30%


Computer Games, 2 students per group

Attendance in talk 5%
Don’t loss these marks !

Final exam 50%


By experience, quite a lot of coding.!
I will give you exercises.

Prepared by Brian Lam Tak Cheung 3


C++ Basics

C++ File Types


Header files (*.h)
Source files (*.cpp, *.c)

Compilation
In Unix: g++ [source.cpp] -o [output_file]
In Visual Studio: press the compile button
We test the assignments in Unix platform if no explicitly specified.

Prepared by Brian Lam Tak Cheung 4


C++ Basics

Can we write C++ as C?


Yes and No
Yes, C++ is backward compatible with C
No, it wastes the OO characteristics of C++

So, what are the characteristics of OO?


Abstraction -- Create a Interface to access the object
Encapsulation -- Protect the data inside object
Polymorphism -- Access different objects or classes with the same
interface
Inheritance -- Reuse of object

Prepared by Brian Lam Tak Cheung 5


C++ Basics

Why OO?
Model the problem domain which is closer to the real world situation
Decompose the the complex problem into many simpler problems
Different parts/objects of the software can be developed by different
people separately, with low dependency
Code can be reused, which lower the cost and time.
Software can be flexibly modified without large change of code by
add-on, remove, re-arrange or modify limited number of objects,
without affecting other objects.
OO is just like playing LEGO

Prepared by Brian Lam Tak Cheung 6


Differences between class and struct
In C, we can use struct to define the data structure
struct struct_name
{
data_type data_variable;
……
}

In C++, class is very similar to struct, and it also include Member Function and
Access Right Label (Private, Public, Protected).

class class_name
{
Private:
data_type data_variable;
……
Public:
void function()
}

Prepared by Brian Lam Tak Cheung 7


Class Definition

Class Definition divide into two parts


Class Declaration
Data member - A data structure inside the class
Member function - A interface to access data member in the same
class
Class Implementation
Implementation the Member functions

 In general, Class declaration is stored in *.h file and Class implementation


is stored in *.cpp file

Prepared by Brian Lam Tak Cheung 8


Example

For the course IEG 3080, there are three Tutors and an
Instructor. Write a program by C++, so that the
Instructor can judge which tutor is the best tutor.
 Step One: Think which object involved
 Step Two: What information or characteristics bound to the object
 Step Three: Declare the Class (the interface)
 Step Four: Implement the functions.

Prepared by Brian Lam Tak Cheung 9


Class Declaration
// store in *.h files
Class Name class Instructor {
private:
string name;
Tutor *tutor;
Keyword “private”, “public” public:
and “protected” is used to Instructor();
Instructor(string name_in, Tutor t0, Tutor t1, Tutor t2);
define the access right to void judge();
the Data Member and ~Instructor();
Member Function };
class Tutor {
private:
Data Member string name;
int marks;
public:
Tutor();
Tutor(string name_in, int marks_in);
Member Function int getMarks();
string getName();
};

Prepared by Brian Lam Tak Cheung 10


Private or Public

There are 3 different access right in C++


(Public, Private and Protected)
Public
Public member (data member or member function) can be accessed
from anywhere.
Private
Private member can be accessed by the member (same class) only.
Protected
Protected member can be accessed by parent class and derived class.
(for inheritance case)

Prepared by Brian Lam Tak Cheung 11


Public or Private

 In general, Data Members are declared under Private area.


Why?
 Reason: Control data access right in order to avoid access data
carelessly (Data Encapsulation)
 In C, we can use pointer to access every data inside program
(No restriction).
 Member Functions are declared under Public Area
(Abstraction). Otherwise, the program can’t call the member
function outside the class. (No Interface)

Prepared by Brian Lam Tak Cheung 12


Class Implementation (Constructors)

 Constructor is used to initialize the value when the class


object is created.
 For struct, we can initialize the value like this
struct Tutor
{
char name[20];
int marks;
}
struct Tutor brian = {“Brian”, 100};
 We can’t initialize class object in this manner. So we use
constructor to initialize the object.

Prepared by Brian Lam Tak Cheung 13


Class Implementation (Constructors)
 Constructor is a function which the name of it is same to the
name of the class.
 Constructor has no return value.
 It may contain more than 1 constructor in the same class. (By
Function Overloading)
 Class_name() is a default Constructor. Same name as the class

Tutor::Tutor() Tutor::Tutor(string name_in, int marks_in = 50)


{ {
name = "Unknown"; name = name_in;
marks = 0; marks = marks_in;
}; };

Prepared by Brian Lam Tak Cheung 14


Class Implementation (Constructors)
Instructor::Instructor()
{
Tutor t0 = Tutor("A"); Code Reuse by calling
Tutor t1 = Tutor("B",100); the other constructor
Tutor t2 = Tutor("C",80);
Instructor("Wing Wong", t0, t1, t2);
};
Instructor::Instructor(string name_in, Tutor t0, Tutor t1, Tutor t2)
{
name = name_in;
tutor = new Tutor[3]; Function Overloading - more than
tutor[0] = t0; one function with same function
tutor[1] = t1; name within a class
tutor[2] = t2;
};

Prepared by Brian Lam Tak Cheung 15


Class Implementation (Constructors)

Create Object Technique


Tutor brian; //ok, Create by default constructor
Tutor brian = Tutor(); //ok, Create by default constructor explicitly
Tutor *brian = new Tutor; //ok, Create an object pointer and call default
//constructor implicitly
Tutor brian(“Brian”,99); // ok, Create by non-default constructor
Tutor brian = Tutor(“Brian”,99); // ok, Create by non-default constructor
Tutor brian(); //What is it?
//It just declare a function in normal way
//Not create a object

Prepared by Brian Lam Tak Cheung 16


Class Implementation (Destructors)

 Only 1 destructor in each class


 Destructor will be called when the object is destroyed
 The name of destructor like “~class_name” with prefix ‘~’
 No return value
 In general, we use destructor to free the memory which is
allocated inside object.

Instructor::~Instructor()
{
Remember to free memory.
delete [] tutor; Otherwise memory leak will
} occur

Prepared by Brian Lam Tak Cheung 17


Class Implementation (Destructors)

Destructor

Prepared by Brian Lam Tak Cheung 18


Class Implementation
// store in *.cpp files void Instructor::judge()
{
int Tutor::getMarks() int mark0 = tutor[0].getMarks();
{ int mark1 = tutor[1].getMarks();
return marks; int mark2 = tutor[2].getMarks();
};
string Tutor::getName() string bestTutor = tutor[0].getName();
{
return name; if(mark1>=mark0)
}; {
bestTutor = tutor[1].getName();
if(mark2>=mark1)
bestTutor = tutor[2].getName();
:: is a scope operator }
else if(mark2>=mark0)
indicating the class bestTutor = tutor[2].getName();
which the function cout << "The best tutor is <" << bestTutor << "> !!" << endl;
belongs to }

Prepared by Brian Lam Tak Cheung 19


Example
main()
{
Tutor t0 = Tutor(“Yamaha",97);
Tutor t1 = Tutor("Brian",99);
Tutor t2 = Tutor(“WildWildWest",98);
Instructor mchang = Instructor("M.Chang",t0,t1,t2);
mchang.judge();
}

Guess the result!!

Prepared by Brian Lam Tak Cheung 20

You might also like