Anatomy of a class Part I

Slides:



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

Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Using Classes to Store Data Computer Science 2 Gerb.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Classes and Objects Systems Programming.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Writing Classes (Chapter 4)
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
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.
Individual Testing, Big-O, C++ Bryce Boe 2013/07/16 CS24, Summer 2013 C.
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Comp1004: Building Better Objects II Encapsulation and Constructors.
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Andrew(amwallis) Classes!
Classes C++ representation of an object
Examples of Classes & Objects
Intro To Classes Review
Creating Your OwnClasses
Anatomy of a class Part II
Operator Overloading CSCE 121 J. Michael Moore
Classes & Objects CSCE 121 J. Michael Moore.
Namespaces CSCE 121 J. Michael Moore
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes & Objects: Examples
Encapsulation and Constructors
Code Organization Classes
Local Variables, Global Variables and Variable Scope
The Object-Oriented Thought Process Chapter 04
Static in Classes CSCE 121 J. Michael Moore.
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Anatomy of Inheritance
Tonga Institute of Higher Education
Introduction to Objects & Classes
JAVA CLASSES.
Object-Oriented Programming
Classes C++ representation of an object
Anatomy of a class Part II
Classes and Objects CGS3416 Spring 2019.
Code Organization Classes
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
Anatomy of a class Part I
Classes and Objects Systems Programming.
Presentation transcript:

Anatomy of a class Part I CSCE 121 J. Michael Moore Based on Slides created by Carlos Soto.

Classes in C++ User-defined datatypes Data members (aka attributes) Variables that are instances of a class datatype are called objects Data members (aka attributes) Can be any datatype Including other classes Member functions (aka methods) Act with or upon the object used to call the method

Visibility Private Public Can only be accessed from within the class Commonly used for most data members Default for C++ classes Public Can be accessed inside or outside the class Commonly used for user-facing functions Make up the class’s interface Default for C++ structs

Class names are capitalized by convention. Writing a class class Student { }; Class names are capitalized by convention.

Writing a class: public and private member access class Student { private: // data members and member functions public: // ... }; Class names are capitalized by convention.

Writing a class: attributes / data members class Student { private: string name; int id; double grade; char letterGrade; public: // data members and member functions };

Writing a class: mutators and accessors / setters and getters class Student { private: string name; int id; double grade; char letterGrade; public: string getName (); void setName (string name); // ... }; Note: member functions are declared, not defined.

Writing a class: function definitions/declarations class Student { string name; // ... public: string getName () { return name; } void setName (string name) { name = name; }; This does not work. Parameter name hides class name. Which name is which???

Writing a class: using ‘implicit’ this parameter class Student { string name; // ... public: string getName () void setName (string name) }; ‘this’ indicates the one that belongs to the class. { return name; } this->name = name; Alternatively you could give them different names. Some will add an underscore to the beginning of one of them. E.g. _name

Writing a class: function definition outside class definition class Student { string name; // ... public: string getName (); void setName (string name); }; string Student::getName () { return name; } Scope Resolution Operator

Using a class Declare/define variable just like any other datatype. class Student { // ... public: string getName(); void setName(string name); }; int main() { Student joe; joe.setName(“Joe Smith”); string name = joe.getName(); } Declare/define variable just like any other datatype. Access attributes/methods with . (dot) operator. Just like using vectors.

Classes & Structs: default visibility class MyClass { // members are private by default }; struct MyStruct { // members are public by default