The C++ Program Structure

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
C++ Classes & Data Abstraction
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
CHAPTER 13 Object Oriented Programming. Objectives  Design class definitions  Implement data hiding and encapsulation  Use accessor and mutator methods.
Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)
. Plab – Tirgul 6 Classes. Point.h class Point { public: Point(int x, int y); ~Point(); int getX() const; int getY() const; private: int m_x, m_y; };
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Lecture 5-6 OOP Overview. Outline A Review of C++ Classes (Lecture 5) OOP, ADTs and Classes Class Definition, Implementation and Use Constructors and.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Classes.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Abstract Data Types and Encapsulation Concepts
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Title of project: pendulum waves. Pendulums Waves Pendulums Waves.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
ECE122 Feb. 22, Any question on Vehicle sample code?
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
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.
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,
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Topics Instance variables, set and get methods Encapsulation
Chapter 3 Part II. 3.8 Placing a Class in a Separate File for Reusability.cpp file is known as a source-code file. Header files ◦ Separate files in which.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
1 C++ Programming Classes ● Class definition and implementation ● Constructors, data members ● Member Functions ● Structuring C + + code.
TK1924 Program Design & Problem Solving Session 2011/2012
Pointer to an Object Can define a pointer to an object:
Classes (Part 1) Lecture 3
Classes C++ representation of an object
3 Introduction to Classes and Objects.
Examples of Classes & Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 5 Classes.
Defining Classes and Methods
Classes and Data Abstraction
Classes & Objects: Examples
Introduction to Classes and Objects
Simple Classes in Java CSCI 392 Classes – Part 1.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CIS 199 Final Review.
NAME 436.
Classes C++ representation of an object
Lecture 8 Object Oriented Programming (OOP)
More C++ Classes Systems Programming.
Classes and Objects Systems Programming.
CSG2H3 Object Oriented Programming
Chapter 5 Classes.
Building Java Programs
Presentation transcript:

The C++ Program Structure The program structure in C++ consists of three parts as follows 1. Class Definition: Each class (or a hierarchy of classes) is defined in a separate “header” file named class_name.h 2. Member Functions Definition: The body of member functions declared in each class are defined in a file named class_name.cpp. This file must contain an include directive to include the code in the header file. 3. Main Program Function: Each program must have a function called main(), needed to instantiate and manipulate objects. This file is called program_name.cpp, and it must also include the header file defining the class.

The C++ Program Structure File: graphics.h File: ClassB.h File: ClassA.h System Library Header Classes Header Files Files Classes Program Files Main Prog File File: Progam_name.cpp #include <graphics.h> #include “ClassA.h” #include “ClassB.h” main() { File: ClassA.cpp #include “ClassA.h” File: ClassB.cpp #include “ClassB.h”

The C++ Program Structure graphics.h is a library header file containing the declarations of graphics functions and variables used in function main(). ClassA.h is a user defined header file containing the declarations for ClassA ClassB.h contains the declarations for ClassB ClassA.cpp contains the definitions of the member functions of classA ClassB.cpp contains the definition of the member functions of ClassB Program_name.cpp contains the definition of of function main().

The C++ Program Structure Example: The PIXEL Program /* point.h--Lab. 1 assignment*/ //describe the file contents // point.h contains two classes: // class Location describes screen locations in X and Y coordinates // class Point describes whether a point is hidden or visible //global data types and data structures declaration enum Boolean {false, true}; //enum is a type specifier for discrete data types, //Boolean is a data type for variables with false and true values

The C++ Program Structure //File pont.h continues // the declaration of class Location class Location { protected: // allows derived class to access private data int X; int Y; public: // these functions can be accessed from outside Location(int InitX, int InitY); // The constructor function int GetX(); // returns the value of X int GetY(); // returns the value of Y };

The C++ Program Structure //File pont.h continues // the declaration of class POINT class Point : public Location { // derived from class Location // public derivation means that X and Y are protected within Point protected: Boolean Visible; // classes derived from Point will need access public: Point(int InitX, int InitY); // constructor void Show(); // make the object visible void Hide(); // make the object invisible Boolean IsVisible(); // returns the value of the variable Visible void MoveTo(int NewX, int NewY); // Change the Location data };

The C++ Program Structure /* File POINT2.CPP--Lab 1 assignment */ // POINT2.CPP contains the definitions for the Point and Location // classes member functions that are declared in the file point.h // start with the include for header files #include <graphics.h> #include "point.h" // member functions for class Location Location::Location(int InitX, int InitY) { X = InitX; Y = InitY; }; int Location::GetX(void) { return X;

The C++ Program Structure // File point2.cpp continues int Location::GetY(void) { return Y; }; // member functions for the Point class: These assume // the main program has initialized the graphics system Point::Point(int InitX, int InitY) : Location(InitX,InitY) { Visible = false; // make invisible by default void Point::Show(void) { Visible = true; putpixel(X, Y, getcolor()); // uses default color

The C++ Program Structure // File point2.cpp continues void Point::Hide(void) { Visible = false; putpixel(X, Y, getbkcolor()); // uses background color to erase }; Boolean Point::IsVisible(void) { return Visible; void Point::MoveTo(int NewX, int NewY) { Hide(); // make current point invisible X = NewX; // change X and Y coordinates to new location Y = NewY; Show(); // show point at new location

The C++ Program Structure /* File: PIXEL.CPP--Lab 1 assignment*/ // PIXEL.CPP demonstrates the Point and Location classes // compile with POINT2.CPP and link with GRAPHICS.LIB //start by including header files #include <graphics.h> // declarations for graphics library #include <conio.h> // for getch() function #include "point.h" // declarations for Point and Location classes int main() { // initialize the graphics system int graphdriver = DETECT, graphmode; initgraph(&graphdriver, &graphmode, "..\\bgi"); // the bgi directory is in under the // turbocpp directory.

The C++ Program Structure // File pixel.cpp continues //instantiate an object called Apoint, make it visible, and then make it move // itself across the screen Point APoint(100, 50); // Initial X, Y location is at 100, 50 APoint.Show(); // APoint turns itself on getch(); // Wait for keypress APoint.MoveTo(300, 150); // APoint moves to 300,150 APoint.Hide(); // APoint turns itself off closegraph(); // Restore original screen return 0; }