Andrew(amwallis) 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

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2014.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
1 CSC241: Object Oriented Programming Lecture No 02.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
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.
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Classes C++ representation of an object
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?
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
EECs 183 Discussion 10 Hannah Westra.
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 I
Templates.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Lecture 11 C Parameters Richard Gesick.
CSC 113 Tutorial QUIZ I.
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
An Introduction to Java – Part II
Simple Classes in C# CSCI 293 September 12, 2005.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Encapsulation and Constructors
Chapter 9 Objects and Classes
Code Organization Classes
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
CS2011 Introduction to Programming I Objects and Classes
Simple Classes in Java CSCI 392 Classes – Part 1.
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming
Review of Previous Lesson
Review of Previous Lesson
Classes C++ representation of an object
Pointers and dynamic objects
Data Structures and ADTs
Classes and Objects CGS3416 Spring 2019.
slides created by Ethan Apter and Marty Stepp
Class rational part2.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
References Revisted (Ch 5)
Code Organization Classes
Anatomy of a class Part I
(4 – 2) Introduction to Classes in C++
Object-Oriented Programming and class Design
Object-Oriented Design AND CLASS PROPERTIES
Introduction to Classes and Objects
Presentation transcript:

Andrew(amwallis) Classes! Discussion 9 Andrew(amwallis) Classes!

Classes!! My favorite topic 

Definition of a class How many different definitions can we come up with?

What is a class? A class is a container that can hold different types of objects (objects with different data types) What is another type of container we’ve learned, where all objects must be the same data type?

What is a class? A class is a container that can hold different types of objects (objects with different data types) What is another type of container we’ve learned, where all objects must be the same data type? An Array

What is a class? A class is a container that can hold different types of objects (objects with different data types) A class is a user-defined data type Just like int and double and string are data types, so is the class you define A class is a way to group together related information

How to think about classes I like to think of a class as an object that holds multiple pieces of information You pass around a box that holds some more information inside it If you have access to the box, you also have access to what’s inside it Do backpack example Backpack is the class or object The computer exists as

How to think about classes Most of you have a backpack with a computer and notebook inside So far in EECS183 you could hold a notebook in one variable, and a computer in the other But this means you you would have to carry the computer and notebook separately But most likely you’d want a backpack to store them in The backpack is the class – you create an object to store related items together By ‘opening’ the backpack you can get access to the computer and notebook

A Student What are some defining characteristics of a student?

A Student What are some defining characteristics of a student? uniqname umid year

Let’s create a Student class What are some defining characteristics of a student? uniqname umid year A student has all of these attributes, so we could make a Student class, where these attributes are member variables

Let’s make a Student class What are some defining characteristics of a student? uniqname umid A student has these attributes, so we could make a Student class, where these attributes are member variables This allows us to declare a Student (our custom data type) that holds attributes of the student as well

Student class class Student{ //what goes in here? };

Student class class Student{ private: string name; int age; public: Student(); Student(string name_in); string getName(); int getUmid(); void setUmid(int umid_in); //setter to set umid }; What are each of these?

Student class class Student{ private: string name; //private member variable int umid; //private member variable public: Student(); //default constructor Student(string name_in); //non-default constructor string getName(); //getter to get name int getUmid(); //getter to get umid void setUmid(int umid_in); //setter to set umid };

Public vs Private A private member variable or member function means that only members of the class can access it Member variables are usually private, and we use setters and getters to access them A public member variable or function means that any part of the code can access that function or variable. Setters and getters are always public.

How do we know that a member function is a constructor? There are 3 ways to tell 1) The function name is exactly the same as the class name 2) The function has no return type 3) The function starts with a capital letter (not definitive) How do we know which type of constructor it is? If it has no parameters (eg. Student()) then it is a default constructor If is has parameters then it is a non-default constructor

Declaring an object of type Student How would we declare and initialize this Student object? int main(){ }

Declaring an object of type Student How would we declare and initialize this Student object? int main(){ Student student1 = Student(); //default constructor Student s2 = Student(“Johnny”); //non-default constructor s2.setUmid(12345678); //set umid } Why couldn’t we say: s2.umid = 12345678?

Declaring an object of type Student How would we declare and initialize this Student object? int main(){ Student student1 = Student(); //default constructor Student s2 = Student(“Johnny”); //non-default constructor s2.setUmid(12345678); //set umid } Why couldn’t we say: s2.umid = 12345678? Because age is a private member variable

Header files vs source files Header files have the .h extension Source files have the .cpp extension Header files are where class definitions and function declarations go Source files are where function implementations go

In the Header file (.h) Some member functions A default constructor A non-default constructor Getters Setters Other functions that perform tasks Public and private members You will often see…

.cpp file Has all the implementation of the declaration in the header file Makes use of the scope resolution operator ::

What goes in a header file? class Student{ //class definition //as defined a few slides ago };

What goes in the source file Member function implementations Don’t forget the scope resolution operator :: class:: says “the following function is a member function of class ----- Meeting Notes (3/8/16 13:56) ----- fix scope resolution operator on the setter

What goes in the source file #include “Student.h” Student::Student (){…} //default constructor Student::Student(string name_in){…} //non-default string Student::getName(){ … } int Student::getUmid(){ void Student::setUmid (int umid_in){ ----- Meeting Notes (3/8/16 13:56) ----- fix scope resolution operator on the setter

Write our own class we will have: Header File .cpp file with implementations main.cpp to make use of our class Think about some real world examples or fun ideas that lend themselves to a class definition

In the Header file (.h) Some member functions including a print function Public and private members A default constructor A non-default constructor Getters Setters

.cpp file Has all the implementation of the declaration in the header file Be careful with syntax here!!!

In main.cpp we want to… Make 3 instances of the class Call one with the default constructor Call the others with the non-default constructors Access elements and change them, probably using getters and setters Do something cool… Our final product will be uploaded to discussion resources along with these slides

Final Product Our final product will be uploaded to discussion resources along with these slides Think about ways you could improve our code!

Feel free to ask any questions after class!