Lecture05-Class_Part_2 - Introduction to Object-Oriented...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Programming II 1 Introduction to Object-Oriented Programming Classes 2 Lecture 5
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Computer Programming II 2 Learning Objectives: To understand how to declare and use constructors and destructors To understand how to declare Queries methods and Updates methods To understand how to declare and when to use const attributes, static attributes, static const attributes, const methods, and static methods To understand how to declare and when to use const objects
Background image of page 2
Computer Programming II 3 Methods Recall that objects can have four basic types of behaviors: constructors , queries , updates , and destructors We will discuss each category in more detail in this lecture
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Computer Programming II 4 Constructors A constructor is a special type of method that is used to perform initialization of object/instance attributes (data members) Whenever an instance of a class is created , a constructor is called automatically Constructors cannot be explicitly called . They are invoked automatically whenever an instance is created Constructor has no return value (not even void) 3 types of constructors: default , overloaded , copy A class can have more than one constructor
Background image of page 4
Computer Programming II 5 Constructors Constructors always have the same name as the class name Since there is no return value, we declare a constructor like any other method, but omit a return type class Student { string name; int id; public: Student () ; // Default constructor Student (int id) ; // Overloaded constructor Student (string name) ; // Overloaded constructor Student ; // Copy constructor }; Constructor name is the same as the class name
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Computer Programming II Default Constructors Default constructor is the constructor that is called when no arguments are provided in the instance declaration A class can have only one default constructor We use default constructor to set attributes to initial/default values when no argument is not provided during object creation class Student { string name; int id; public: Student() { // Default constructor, no argument name = "Unknown"; // Set default value id = 0; // Set default value } }; int main() { Student s1 ; // call default constructor, id=0, name="Unknown" Student *ps = new Student ; // call default constructor, id=0, name="Unknown" ...
Background image of page 6
Default Constructors If you do not provide both default constructor and overloaded constructor, C++ automatically creates a default constructor with blank implementation // Your code class Student { // No default constructor and no // overloaded constructor is provided string name; int id; }; // Compiler code class Student { string name; int id; public: Student() { // Compiler's default constructor. // Blank definition/implementation.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/03/2012 for the course IT 1241 taught by Professor Pang during the Spring '11 term at Multimedia University, Cyberjaya.

Page1 / 40

Lecture05-Class_Part_2 - Introduction to Object-Oriented...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online