Object-Oriented Programming (OOP) Lecture No. 14

Slides:



Advertisements
Similar presentations
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Advertisements

1 CSC241: Object Oriented Programming Lecture No 21.
Introduction to Programming Lecture 39. Copy Constructor.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
Before Introducing Inheritance … Here is another way to re-use what have been developed: This is known as the object composition! A real-world example.
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
Constructors & Destructors Review CS 308 – Data Structures.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
 Review structures  Program to demonstrate a structure containing a pointer.
1 CSC241: Object Oriented Programming Lecture No 22.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
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. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 7.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
1 CSE 2341 Object Oriented Programming with C++ Note Set #2.
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
Data Types Storage Size Domain of all possible values Operations 1.
1 CSC241: Object Oriented Programming Lecture No 17.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
Previous lecture Introduction to OOP and C++ Data Abstraction –String example.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
©Copyrights 2016 Eom, Hyeonsang All Rights Reserved Computer Programming Object Oriented Programming & C++ 1 st Lecture 엄현상 (Eom, Hyeonsang) Department.
Object-Oriented Programming (OOP) Lecture No. 24.
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Object-Oriented Programming (OOP) Lecture No. 15
Object Oriented Programming (OOP) Lecture No. 8
CSC241: Object Oriented Programming
Concepts of Constructors and Its Types
Object-Oriented Programming (OOP) Lecture No. 17
LinkedList Class.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Stack and Queues Stack implementation using Array
Stack and Queues Stack implementation using Array
Object-Oriented Programming (OOP) Lecture No. 18
Pointers, Dynamic Data, and Reference Types
Object Oriented Programming (OOP) Lecture No. 9
Introduction to Programming
Object-Oriented Programming (OOP) Lecture No. 25
Object-Oriented Programming (OOP) Lecture No. 20
Object-Oriented Programming (OOP) Lecture No. 22
Introduction to Programming
Function Overloading.
Web Design & Development Lecture 4
CMSC 341 C++ and OOP.
Object-Oriented Programming (OOP) Lecture No. 34
Object Oriented Programming (OOP) Lecture No. 11
Object oriented programming (OOP) Lecture No. 6
Object-Oriented Programming (OOP) Lecture No. 23
CSCE 206 Lab Structured Programming in C
Data Structures (CS301) Linked List and its implementation
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
Object Oriented Programming (OOP) Lecture No. 12
Presentation transcript:

Object-Oriented Programming (OOP) Lecture No. 14

Consider the following implementation of the student class: Composition Consider the following implementation of the student class: Student gpa : float rollNo : int name : char * Student(char * = NULL, int = 0, float = 0.0); Student(const Student &) GetName() const : const char * SetName(char *) : void ~Student() …

Composition class Student{ private: float gpa; char * name; int rollNumber; public: Student(char * = NULL, int = 0, float = 0.0); Student(const Student & st); const char * GetName() const; ~Student(); … };

Composition Student::Student(char * _name, int roll, float g){ cout << "Constructor::Student..\n"; if (!_name){ name = new char[strlen(_name)+1]; strcpy(name,_name); } else name = NULL; rollNumber = roll; gpa = g;

Composition Student::Student(const Student & st){ if(str.name != NULL){ name = new char[strlen(st.name) + 1]; strcpy(name, st.name); } else name = NULL; rollNumber = st.roll; gpa = st.g;

Composition const char * Student::GetName(){ return name; } Student::~Student(){ delete [] name;

Composition C++: “its all about code reuse” Composition: Creating objects of one class inside another class “Has a” relationship: Bird has a beak Student has a name

Composition Conceptual notation: name : String Student gpa : float rollNo : int name : String Student(char * = NULL, int = 0, float = 0.0); Student(const Student &) GetName() const : String GetNamePtr() const : const char * SetName(char *) : void ~Student() … Student String string : char * String() SetString(char *) : void GetString() const : const char * ~String() …

Composition class String{ private: char * ptr; public: String(); String(const String &); void SetString(char *); const char * GetString() const; ~String() … };

Composition String::String(){ cout << "Constructor::String..\n"; ptr = NULL; } String::String(const String & str){ if(str.ptr != NULL){ string = new char[strlen(str.ptr)+1]; strcpy(ptr, str.ptr); else ptr = NULL;

Composition void String::SetString(char * str){ if(ptr != NULL){ delete [] ptr; ptr = NULL; } if(str != NULL){ ptr = new char[strlen(str)+1]; strcpy(ptr, str);

Composition const char * String::GetString()const{ return ptr; } String::~String(){ delete [] ptr; cout <<"Destructor::String..\n";

Composition class Student{ private: float gpa; int rollNumber; String name; public: Student(char* =NULL, int=0,float=0.0); Student(const Student &); void SetName(const char *); String GetName() const; const char * GetNamePtr const(); ~Student(); … };

Composition Student Student(char * _name, int roll, float g){ cout <<"Constructor::Student..\n"; name.SetString(_name); rollNumber = roll; gpa = g; }

Composition Student::Student(const Student & s){ name.Setname(s.name.GetString()); gpa = s.gpa; rollNo = s.rollNo; } const char * Student::GetNamePtr() const{ return name.GetString();

Composition void Student::SetName(const char * n){ name.SetString(n); } Student::~Student(){ cout <<"Destructor::Student..\n";

Composition Main Function: void main(){ Student aStudent("Fakhir", 899, 3.1); cout << endl; cout << “Name:” << aStudent.GetNamePtr() << “\n”; }

Composition Output: Constructor::String.. Constructor::Student.. Name: Fakhir Destructor::Student.. Destructor::String..

Composition Constructors of the sub-objects are always executed before the constructors of the master class Example: Constructor for the sub-object name is executed before the constructor of Student