Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.

Slides:



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

1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.
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.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
CS-2303 System Programming Concepts
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
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.
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Week 5 Lab 1 comments Hand in Lab 2 Questions from Last Week Classes continued Lab.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
1 OOP - An Introduction ISQS 6337 John R. Durrett.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
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.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
Introduction to Programming
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Chapter 3 Introduction to Classes and Objects
Introduction to Classes
group work #hifiTeam
Introduction to Classes
Classes, Constructors, etc., in C++
Scope Rules and Storage Types
9-10 Classes: A Deeper Look.
Introduction to Classes and Objects
Classes C++ representation of an object
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
9-10 Classes: A Deeper Look.
CECS 130 Final Exam Review Session
Classes and Objects Systems Programming.
(4 – 2) Introduction to Classes in C++
Introduction to Classes and Objects
Presentation transcript:

Introduction to C++

Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation Creating objects

C++? Developed by Bjarne Stroustrup C with extentions Object orientation (classes, inheritance, …) Templates References (also as parameter) New (and some better) libraries (,, …) - Operator overloading - … Can be used mixed with C (not recommended) Well-suited to big programming projects

What are references? “An alternative name for an object” An object that shares the data with another object Can only be used on places where the object could also be used void main() { float x = 1.0; float &refx = x;//shares dataspace with x float y = refx;// y has now value of x and refx refx = 15.0; //set 15.0 in dataspace of x }

What are references? Can be used to avoid pointers Handy in functions void swap (int &a, int &b) { //references int temp = a; a = b; b = temp; } Work in actual dataspace of variables NOT a pointer!! References to the same object in his whole scope

Object orientation Work with elements/objects (Car, Robot, …) instead of loose variables C++ program has Main loop (multiple) Classes, objects, … Classes (blueprint of an object) Object (instance of a class, has a reserved dataspace)

Class Blueprint of an object, describes what an object can and stores Define member functions and data members A structure with functions (but default private access rules, see later)

Class class Gradebook{ public: /*Public member functions of the class*/ Gradebook(){ cout << "Enter the name: " ; getline(cin,Owner); } void PrintName(){ cout << "Name of owner of the Gradebook is: " << GetOwner() <<endl; } void SetOwner(string name){ cout << "Setting owner..." << endl; Owner = name; } //accessor: Get the name of the owner string GetOwner(){ return Owner; } private: /*Private data members of the class */ string Owner; }; “class” to start a class Access specifier Member functions Data member Constructor

Access specifiers Define the accessability of member functions and data members Public Default for structures in C++ Can be called from outside the class Available to “the public” No data hidding Used for most member functions Private Default for classes Only accessable from inside the class Data is hidden, data hiding Used for data members or internal member functions Make accessable by using mutators/accessors (public functions to manipulate data members)

Constructor/Destructor Constructor Always called when object is created Same name as class No return type, not even “void” If none defined it is created by the compiler Default without arguments Can be overloaded

Constructor/Destructor //Constructor with argument Gradebook(string name){ Owner = name; } //Constructor without arguments Gradebook(){ cout << "Enter the name: " ; getline(cin,Owner); }

Constructor/Destructor Destructor Always called when object is deleted Same name as class but “~” in front Used to clean up memory (linked lists, …), close files, … Created by compiler if not defined

Constructor/Destructor ~Gradebook(){ cout << "Calling destructor of the Gradebook class..." << endl; }

Interface-Implementation separation Make your classes reusable Define in separate files Interface Only declarations of functions Data members Implementation Actual function code Use “classname::functionname”

Interface /* Gradebook.h: Interface of the Gradebook class */ class Gradebook{ public: /*Public member functions of the class*/ //Constructors Gradebook(string name); Gradebook(); //destructor ~Gradebook(); void PrintName(); //mutator void SetOwner(string name); //accessor string GetOwner(); private: /*Private data members of the class */ string Owner; //owners name };

Implementation /* Gradebook.cpp*/ Gradebook::Gradebook(string name){ Owner = name; } Gradebook::Gradebook(){ cout << "Enter the name: " ; getline(cin,Owner); } void Gradebook::PrintName(){ cout << "Name of owner of the Gradebook is: " << GetOwner() <<endl; } void Gradebook::SetOwner(string name){ cout << "Setting owner..." << endl; Owner = name; } string Gradebook::GetOwner(){ return Owner; } //destructor Gradebook::~Gradebook(){ cout << "Calling destructor of the Gradebook class..." << endl; }

Creation of objects Automatic class object Object is created like any other variable (Gradebook G1;) Memory allocated on the stack Deleted when the scope ends Using new-operator Memory dynamic allocated Result is pointer to allocated memory (Gradebook *G2 = new Gradebook();) Need to use delete-operator to free this memory (comparable to malloc-free in C)

Example

References C++ for Programmers by Paul J. Deitel The C++ Programming Language: Special Edition by Bjarne Stroustrup

Questions?