CSC241 Object-Oriented Programming (OOP) Lecture No. 6.

Slides:



Advertisements
Similar presentations
Copyright  Hannu Laine C++-programming Part 7 Hannu Laine Static members Different uses of const specifier.
Advertisements

Structures Spring 2013Programming and Data Structure1.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Objects Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
TCP1201 OOPDS 1 Class & Template Lecture 2. TCP1201 OOPDS 2 Learning Objectives: To understand object behaviors To understand constructors To understand.
Classes Joe Meehean. Complex Data Types What if we need more complex data types? not simple primitives like ints, float, and chars data types that are.
Object-Oriented Programming in C++
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming (OOP) Lecture No. 10.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 7.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
More C++ Features True object initialisation
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Function Overloading and References
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Programming Techniques Classes II Important Class Features Spring 2009.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Eine By: Avinash Reddy 09/29/2016.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Object Oriented Programming (OOP) Lecture No. 8
Review: Two Programming Paradigms
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
This technique is Called “Divide and Conquer”.
Object Oriented Programming (OOP) Lecture No. 9
Local Variables, Global Variables and Variable Scope
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Object oriented programming (OOP) Lecture No. 7
Submitted By : Veenu Saini Lecturer (IT)
CS410 – Software Engineering Lecture #5: C++ Basics III
Object Oriented Programming (OOP) Lecture No. 11
Object oriented programming (OOP) Lecture No. 6
Types of Computer Languages
Object Oriented Programming (OOP) Lecture No. 12
Object Oriented Programming (OOP) Lecture No. 10
Presentation transcript:

CSC241 Object-Oriented Programming (OOP) Lecture No. 6

Review  this Pointer  Separation of interface and implementation  Constant member functions

Problem  Change the class Student such that a student is given a roll number when the object is created and cannot be changed afterwards

Student Class class Student{ … int rollNo; public: Student(int aNo); int getRollNo(); void setRollNo(int aNo); … };

Modified Student Class class Student{ … const int rollNo; public: Student(int aNo); int getRollNo(); void setRollNo(int aNo); … };

Example Student::Student(int aRollNo) { rollNo = aRollNo; /*error: cannot modify a constant data member*/ }

Example void Student::setRollNo(int i) { rollNo = i; /*error: cannot modify a constant data member*/ }

Member Initializer List  A member initializer list is a mechanism to initialize data members  It is given after closing parenthesis of parameter list of constructor  In case of more then one member use comma separated list

Example class Student{ const int rollNo; char *name; float GPA; public: Student(int aRollNo): rollNo(aRollNo), name(Null), GPA(0.0){ … } … };

Order of Initialization  Data member are initialized in order they are declared  Order in member initializer list is not significant at all

Example class ABC{ int x; int y; int z; public: ABC(); };

Example ABC::ABC() :y(10), x(y), z(y) { … } /*x = Junk value y = 10 z = 10*/

const Objects  Objects can be declared constant with the use of const keyword  Constant objects cannot change their state

Example int main() { const Student aStudent; return 0; }

Example class Student{ … int rollNo; public: … int getRollNo(){ return rollNo; } };

Example int main(){ const Student aStudent; int a = aStudent.getRollNo(); //error }

const Objects  const objects cannot access “non const” member function  Chances of unintentional modification are eliminated

Example class Student{ … int rollNo; public: … int getRollNo()const{ return rollNo; } };

Example int main(){ const Student aStudent; int a = aStudent.getRollNo(); }

Constant data members  Make all functions that don’t change the state of the object constant  This will enable constant objects to access more member functions

Static Variables  Lifetime of static variable is throughout the program life  If static variables are not explicitly initialized then they are initialized to 0 of appropriate type

Example void func1(int i){ static int staticInt = i; cout << staticInt << endl; } int main(){ func1(1); func1(2); } Output: 1

Static Data Member Definition “A variable that is part of a class, yet is not part of an object of that class, is called static data member”

Static Data Member  They are shared by all instances of the class  They do not belong to any particular instance of a class

Class vs. Instance Variable  Student s1, s2, s3; Class Space s1 (rollNo,…) s2 (rollNo,…) s3 (rollNo,…) Instance Variable Class Variable

Static Data Member (Syntax)  Keyword static is used to make a data member static class ClassName{ … static DataType VariableName; };

Defining Static Data Member  Static data member is declared inside the class  But they are defined outside the class

Defining Static Data Member class ClassName{ … static DataType VariableName; }; DataType ClassName::VariableName;

Initializing Static Data Member  Static data members should be initialized once at file scope  They are initialized at the time of definition

Example class Student{ private: static int noOfStudents; public: … }; int Student::noOfStudents = 0; /*private static member cannot be accessed outside the class except for initialization*/

Initializing Static Data Member  If static data members are not explicitly initialized at the time of definition then they are initialized to 0

Example int Student::noOfStudents; is equivalent to int Student::noOfStudents = 0;

Review  Constant data members  Constant objects  Static data members

Static Data Member Definition “A variable that is part of a class, yet is not part of an object of that class, is called static data member”

Static Data Member  They are shared by all instances of the class  They do not belong to any particular instance of a class

Class vs. Instance Variable  Student s1, s2, s3; Class Space s1 (rollNo,…) s2 (rollNo,…) s3 (rollNo,…) Instance Variable Class Variable

Static Data Member (Syntax)  Keyword static is used to make a data member static class ClassName{ … static DataType VariableName; };

Defining Static Data Member  Static data member is declared inside the class  But they are defined outside the class

Defining Static Data Member class ClassName{ … static DataType VariableName; }; DataType ClassName::VariableName;

Initializing Static Data Member  Static data members should be initialized once at file scope  They are initialized at the time of definition

Example class Student{ private: static int noOfStudents; public: … }; int Student::noOfStudents = 0; /*private static member cannot be accessed outside the class except for initialization*/

Initializing Static Data Member  If static data members are not explicitly initialized at the time of definition then they are initialized to 0

Example int Student::noOfStudents; is equivalent to int Student::noOfStudents = 0;

Accessing Static Data Member  To access a static data member there are two ways  Access like a normal data member  Access using a scope resolution operator ‘::’

Example class Student{ public: static int noOfStudents; }; int Student::noOfStudents; int main(){ Student aStudent; aStudent.noOfStudents = 1; Student::noOfStudents = 1; }

Life of Static Data Member  They are created even when there is no object of a class  They remain in memory even when all objects of a class are destroyed

Example class Student{ public: static int noOfStudents; }; int Student::noOfStudents; int main(){ Student::noOfStudents = 1; }

Example class Student{ public: static int noOfStudents; }; int Student::noOfStudents; int main(){ { Student aStudent; aStudent.noOfStudents = 1; } Student::noOfStudents = 1; }

Uses  They can be used to store information that is required by all objects, like global variables

Example  Modify the class Student such that one can know the number of student created in a system

Example class Student{ … public: static int noOfStudents; Student(); ~Student(); … }; int Student::noOfStudents = 0;

Example Student::Student(){ noOfStudents++; } Student::~Student(){ noOfStudents--; }

Example int Student::noOfStudents = 0; int main(){ cout << Student::noOfStudents << endl; Student studentA; cout << Student::noOfStudents << endl; Student studentB; cout << Student::noOfStudents << endl; } Output: 0 1 2

Problem  noOfStudents is accessible outside the class  Bad design as the local data member is kept public

Static Member Function Definition: “The function that needs access to the members of a class, yet does not need to be invoked by a particular object, is called static member function”

Static Member Function  They are used to access static data members  Access mechanism for static member functions is same as that of static data members  They cannot access any non-static members

Example class Student{ static int noOfStudents; int rollNo; public: static int getTotalStudent(){ return noOfStudents; } }; int main(){ int i = Student::getTotalStudents(); }

Accessing non static data members int Student::getTotalStudents(){ return rollNo; } int main(){ int i = Student::getTotalStudents(); /*Error: There is no instance of Student, rollNo cannot be accessed*/ }

this Pointer  this pointer is passed implicitly to member functions  this pointer is not passed to static member functions  Reason is static member functions cannot access non static data members

Global Variable vs. Static Members  Alternative to static member is to use global variable  Global variables are accessible to all entities of the program  Against information hiding

Array of Objects  Array of objects can only be created if an object can be created without supplying an explicit initializer  There must always be a default constructor if we want to create array of objects

Example class Test{ public: }; int main(){ Test array[2]; // OK }

Example class Test{ public: Test(); }; int main(){ Test array[2]; // OK }

Example class Test{ public: Test(int i); }; int main(){ Test array[2]; // Error }

Example class Test{ public: Test(int i); } int main(){ Test array[2] = {Test(0), Test(0)}; }

Example class Test{ public: Test(int i); } int main(){ Test a(1), b(2); Test array[2] = { a, b }; }