Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
C++ Classes & Data Abstraction
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
C++ data types. Structs vs. Classes C++ Classes.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
IT PUTS THE ++ IN C++ Object Oriented Programming.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Inheritance using Java
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Lecture 10 Inheritance “Absolute C++” Chapter 14.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ECE122 Feb. 22, Any question on Vehicle sample code?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
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 Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Object-Oriented Programming in C++ More examples of Association.
Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,
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.
Structures, Classes and Objects Handling data and objects Unit - 03.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
1 CSC241: Object Oriented Programming Lecture No 02.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
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.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Classes, Interfaces and Packages
1 Introduction to Object Oriented Programming Chapter 10.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Andrew(amwallis) Classes!
Classes C++ representation of an object
Anatomy of a class Part II
Templates.
Lecture 9 Concepts of Programming Languages
Defining Your Own Classes Part 1
Basic C++ What’s a declaration? What’s a definition?
Simple Classes in C# CSCI 293 September 12, 2005.
Simple Classes in Java CSCI 392 Classes – Part 1.
Object-Oriented Programming
Constructors Member Functions Inheritance
Classes C++ representation of an object
Anatomy of a class Part II
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS148 Introduction to Programming II
C++ data types.
Class Circle { Float xc, yc, radius;
SPL – PS3 C++ Classes.
Lecture 9 Concepts of Programming Languages
Chengyu Sun California State University, Los Angeles
Introduction to Classes and Objects
Presentation transcript:

Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick

Classes – declaration & definition Just like C++ global functions and global variables, C++ classes must be declared before they are used C++ classes must (generally) be implemented in two parts Declaration – in a header file Definition – in a .cpp file CS-1030 Dr. Mark L. Hornick

A C++ Class Declaration (in .h) class keyword Name of the class class Employee { private: string name; int id; public: Employee(string nameArg, int idArg ); string getName(); int getID(); }; Attributes cannot be initialized Don’t forget the semi-colons! CS-1030 Dr. Mark L. Hornick

Class Definition Notes public Visible to everyone private Visible only within the class Used to protect class objects protected Visible to classes which derive from the base class In Java, protected attributes and methods are also visible to other classes in the same package as well CS-1030 Dr. Mark L. Hornick

Defining a Class Member Function (in .cpp) function’s full name return type function’s scope int Employee::getID () { return id; } Employee::Employee( string nameArg, int idArg ) { name = nameArg; id = idArg; } CS-1030 Dr. Mark L. Hornick

Class Constructors Job is to initialize the object Called automatically, just like in Java Here is where attributes are initialized Same name as the class Just like Java No return type (not even void) Java constructors have the void type Can be overloaded CS-1030 Dr. Mark L. Hornick

Parameter names optional in declaration! Constructor Example Declaration in .h file: class Employee { // note no “public” public: Employee(string nameArg, int idArg); …}; Definition in .cpp file: Employee::Employee(string name, int id) { // initialization code here } Parameter names optional in declaration! No return value CS-1030 Dr. Mark L. Hornick

Creating an instance of a C++ class #include “employee.h” int main( int argc, char* argv[] ){ // note difference vs. Java: Employee pat( “Pat”, 123 ); Employee tim( “tim”, 456 ); int id1 = pat.getID(); // objects are destroyed here… } CS-1030 Dr. Mark L. Hornick

C++ Class Destructor No such method in Java Cleans up an object at the end of lifetime Cleans up the data members prior to their own destruction Closes things (like files), etc. Called automatically Special name – similar to constructor Clock::~Clock() // note the ~ If you don’t supply a destructor, the compiler will! Compiler-supplied destructor does nothing! CS-1030 Dr. Mark L. Hornick

No arguments or return value Destructor Example In .h file class Employee {… public: ~Employee(); …}; In .cpp file Employee::~Employee() {} // Do nothing No arguments or return value CS-1030 Dr. Mark L. Hornick