Code Organization Classes

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
Using Classes to Store Data Computer Science 2 Gerb.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
Week 4 Recap CSE 115 Fall 2006 Section C. Decoupling Separation of concerns Defining what an object can do, not how it does it.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Separating Definition & Implementation Headers and Comments.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
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.
Object Oriented Programming (OOP) Lecture No. 10.
Week 11 Multi-file Programs and Scope of Variables.
Object Oriented Programming COP3330 / CGS5409.  Inheritance  Assignment 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?
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Classes in C++ By Ms Nashandi. Placing a class in a separate file for Reusability When building an object C++ Program, it is customary to define reusable.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
TK1924 Program Design & Problem Solving Session 2011/2012
Andrew(amwallis) Classes!
Template Classes.
Classes C++ representation of an object
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?
Templates C++ template
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Classes Object-oriented programming: Example: Bank transactions
C++ Object-Oriented Programming
Chapter 7: Introduction to Classes and Objects
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?
Anatomy of a class Part II
Anatomy of a class Part I
Lecture 4-7 Classes and Objects
Separate Compilation and Namespaces
Structures putting data together.
Chapter 9 Objects and Classes
Separating Definition & Implementation
Code Organization Classes
Static in Classes CSCE 121 J. Michael Moore.
Code Organization CSCE 121 J. Michael Moore.
Classes.
Namespaces How Shall I Name Thee?.
Submitted By : Veenu Saini Lecturer (IT)
Classes C++ representation of an object
Anatomy of a class Part II
Classes “Absolute C++” Chapter 6
Templates C++ template
Anatomy of a class Part I
Classes and Objects Systems Programming.
Object Oriented Programming
Introduction to Classes and Objects
Presentation transcript:

Code Organization Classes CSCE 121

Recall Definitions can only happen once. We can separate functions into different files. #include related header file We will do something similar with Classes!

Recall Method / member function definitions can be separated from function declaration. class Student { string name; // ... public: string getName (); void setName (string _name); }; string Student::getName () { return name; } Consequently, they can be placed into separate files. Or we can leave the function declaration inside the class, but define (or implement) them outside. To do this, we need to use the scope resolution operator. Notice that the return type still goes at the very beginning, and that the actual name of this function is Student::getName(); This distinguishes from other functions of the same name. And also notice that since we specified that we are editing a member function of our class, we can directly access its private data members.

For classes Filename (.cpp and either .h or .hpp) should be name of the class. Start with a Capital Letter Header File Class definitions Method / member function declarations Class File Class method / member function definitions. Use scope resolution operator Header guards are REALLY important here. These header files contain definitions!

Class in Separate Files Student.h #ifndef STUDENT_H #define STUDENT_H class Student { string name; int id; public: Student (string name, int id); string getName (); void setName (string name); }; #endif Student.cpp #include "Student.h" Student::Student (string name, int id): name(name), id(id) {} string Student::getName () { return name; } void Student::setName(string name) { this->name = name; Or we can leave the function declaration inside the class, but define (or implement) them outside. To do this, we need to use the scope resolution operator. Notice that the return type still goes at the very beginning, and that the actual name of this function is Student::getName(); This distinguishes from other functions of the same name. And also notice that since we specified that we are editing a member function of our class, we can directly access its private data members.