C++ CLASSES C/C++ ADVANCED PROGRAMMING

Size: px
Start display at page:

Download "C++ CLASSES C/C++ ADVANCED PROGRAMMING"

Transcription

1 C++ CLASSES C/C++ ADVANCED PROGRAMMING

2 GOAL OF THIS LECTURE C++ classes Dr. Juan J. Durillo 2

3 (C++) CLASSES: BASIC CONCEPTS Fundamentals of classes data abstraction data encapsulation Data abstraction: separation interface/implementation interface: operations that customers of the class can execute implementation: class data members + function bodies (interface implementation and internal ones) Encapsulation enforces the separation of a class interface and implementation users of the class can use the interface but they have no access to the implementation A class (using data abstraction and encapsulation) defines an abstract data type The class designer worries about how the class is implemented Clients of the class only need to know only how the abstract type works (defined by the interface) Dr. Juan J. Durillo 3

4 (C++) CLASSES: BASIC CONCEPTS Abstract data types are defined by their interface rather than their data Class designers decide which methods are visible to clients which methods are intended for the implementation of the class data types used to provide that implementation The interface of a class consist of its public members Dr. Juan J. Durillo 4

5 C++ CLASSES: BASICS Basic class definition struct myclass { string value() const; myclass& combine(const Sales_data&); double price() const; }; Note as in java, this can be used to refer to the current object the type of this is a const pointer A reference to this can be returned by using return *this Classes provide member functions and operators Member functions are declared similarly to ordinary functions must be declared inside the class may be defined inside the class itself or outside the class body Note examples of operators can be +,=, <<, >>, or += Note functions defined in the class are implicitly inline Dr. Juan J. Durillo 5

6 THE STRUCT AND CLASS KEYWORDS Within a class body we use different access specifiers Members declared after a public specifier are accessible to all parts of the program Members declared after a private specifier are accessible to the member functions of the class, but are not accessible to code that uses the class There is no restriction on how many times a specifier can appear in the body of a class Classes can be declared with the keywords struct or class struct: all the members declared before the first specifier are publics class: all the members declared before the first specifier are private 6

7 THE FRIEND ACCESS SPECIFIER A class can allow another class or function to access its non-public members by tagging that class or function with the friend specifier A class makes a function its friend by including a declaration for that function preceded by the keyword friend Friends declarations may appear only inside a class declaration anywhere in the class, but not outside they are not member of the class and are not affected by the access control of the section in which they are declared Dr. Juan J. Durillo 7

8 THE FRIEND ACCESS MODIFIER Declaring a friend class struct myclass { friend class OtherClass; }; Declaring a friend function struct myclass { friend void OtherClass::clear(); // declaring the function clear as friend }; Note The right sequence of steps are: 1. Define OtherClass and declare (but not define) clear 2. Include a friend member for clear 3. Define clear Although they share a common name, overloaded versions of a function are still different functions Different overloaded functions variants would require different friend declarations Dr. Juan J. Durillo 8

9 C++ CLASSES: BASICS Methods may include the keyword const following the parameter list (see example in previous slide) purpose: to modify the type of the implicit this pointer Explanation this would be of the type myclass *const (in the example before), i.e., a const pointer A pointer to a non const object cannot point to a const object (because we would be able to modify the object then) It would not make sense that this would point to a const objects (const myclass* const in the example before) To avoid that the keyword const transforms the this for that call into the type const myclass*const These methods cannot modify the object they work on These methods are known as const member methods Note objects that are const, and references or pointers to const objects, may call only const member functions Dr. Juan J. Durillo 9

10 MUTABLE DATA MEMBER They keyword mutable allows to modify data members inside const member functions A const member function may change a mutable data member (actually any member function can change its value) A mutable data member is never const, even when it is a member of const object Dr. Juan J. Durillo 10

11 CLASS SCOPE AND MEMBER FUNCTIONS A class defines a scope, where definition of the member functions are nested within When a function member is defined outside a class The return type, parameter list, and name must match the declaration in the class body If the member was declared as a const member function, then the definition must also specify const after the parameter list The name of a member defined outside a class must include the name of the class of which it is a member Dr. Juan J. Durillo 11

12 CLASSES LIFECYCLE There are a few basic operations that any class is likely to need to support Initialization Cleaning up memory or other resources Copying itself Assigning it Moving? Dr. Juan J. Durillo 12

13 CREATING INSTANCES OF A CLASS Dr. Juan J. Durillo 13

14 CONSTRUCTORS Each class defines how objects of its type can be created special member functions known as constructors same name as the class, have no return type, and have a parameter list a class may have multiple constructors which differ in the number of type parameters Constructors role: to initialize data members of a class object Constructors cannot be declared as const during the initialization of an object the object is not const Note this does not mean that you cannot create a const object! Actually you can (and sometimes you must!) 14

15 (SYNTHESIZED) DEFAULT CONSTRUCTOR The default constructor is one that uses no parameters (empty parameter list) A constructor defining default values for all its parameters is also a default constructor When no constructor is provided, the compiler will synthesize a default constructor If a class defines a constructor with a parameter, that class have no default constructor unless a default constructor is explicitly defined The synthesized default constructor initializes each class members as follows If the type to initialize has a in-class initializer, use it Otherwise, default-initialize each of the member Dr. Juan J. Durillo 15

16 SYNTHESIZED DEFAULT CONSTRUCTOR Only fairly simple classes should rely on the synthesized default constructor The synthesized constructor may not always do the right thing the class designer knows better than the compiler how she wants the object to be initialized Yet sometimes it may do the right thing, and we can tell the compiler to generate it (C++11) struct myclass { myclass() = default; myclass(const std::string &s) : message(s) {} }; Sometimes the compiler is unable to generate a default constructor E.g., the class has a member of a particular class type with no default constructor Dr. Juan J. Durillo 16

17 EXPLICIT CALLS TO THE DEFAULT CONSTRUCTOR The default constructor takes an empty list of parameters; however, the following declaration is erroneous Sales_data obj(); Although it compiles, it declares a function, it does not create an object The declared function takes no arguments and returns a object of the class type The correct way of defining an object that uses the default constructor for initialization is to leave off the trailing empty parenthesis Sales_data obj Dr. Juan J. Durillo 17

18 THE DEFAULT CONSTRUCTOR ROLE Is used whenever an object is default or value initialized Default initialization occurs when we define non static variables or array at block scope without initializers a class has member of a class type, its default constructor is called by the synthesized default constructor a class member is not explicitly initialized in a constructor initializer list Value initialization occurs when array is initialized when we provide fewer initializers than the size of the array we define a local static object without an initializer we explicitly request a value initialization by writing an expression of the form T(), being T a type name Classes must have a default constructor in order to be used in these contexts Dr. Juan J. Durillo 18

19 CONSTRUCTOR INITIALIZATION LIST After the parameter list, a constructor sometimes includes a comma-separated list starting with a colon (:) that comma-separated list is known as the constructor initializer list The constructor initializer list is used to give the initial value to one or more of the object members Each member of the list is a member name followed by the desired initial value for that member in parenthesis (or curly braces) Members ignored by the initializer list are initialized using the in-class value or default initializer if no in class initializer exists Note Constructors should not override in-class initializers except to use a different initial value; if we do not explicitly initialize a member using the constructor initializer list, that member is default initialized before the method start executing. If that member is const, then we cannot assign any other value to that member: Our single chance is to use the constructor initializer list Dr. Juan J. Durillo 19

20 CONSTRUCTOR INITIALIZATION LIST A constructor initializer list specifies only the values to initialize the members, not the order in which those initializations are performed Members are initialized in the order in which they are declared i.e., the first declared member is initialized first, the second next, and so on Although the order of initialization does not matter in many occasions, when a member is initialized in terms of another, the order in which members are initialized is crucially important class X { int i; int j; public: X(int val) : j(val), i(j) {} //undefined: i is initialized before j }; Dr. Juan J. Durillo 20

21 DELEGATING CONSTRUCTORS Since C++11 standard, a constructor initializer list can delegate on another constructor Delegating constructor A delegating constructor uses another constructor from its own class to initialize the object it is said to delegate some (or all) of its work to this other constructor In a delegating constructor, the member initializer list has a single entry that is the name of the class itself followed by a parenthesized list of arguments The argument list must match the list of arguments of another constructor of this class class Sales_data{ public: Sales_data():Sales_data(,0,0) {} }; Dr. Juan J. Durillo 21

22 COPYING, ASSIGNING, AND DESTROYING OBJECTS Classes also control what happens when we copy, assign, or destroy objects of that class Objects are copied in several contexts e.g., when we initialize a variable or when we pass or return an object by value Objects are assigned when we use the assignation operator in anything different than a declaration Objects are destroyed when the class ceases to exist e.g., when a local object is destroyed or exit from the block when it was created e.g., objects in a vector are destroyed when the vector is destroyed If we do not define operations for copying, assigning and destroying an object, the compiler will synthesize them for us In many cases synthesized versions of these operations will do the work for us, some classes, specially if the use dynamic memory, should not rely however on the synthesized versions 22

23 CLASSES DEFINE A TYPE A class can define its own local name for types Control access mechanisms as for any other member apply class myclass { public: typedef std::string::size_type pos; private: pos cursor = 0; }; Note Members that define a type must appear before they are used (as a result type members usually appear at the beginning of the class) class myclass { public: using pos = std::string::size_type; }; Note We define pos in the public part because we want users to use that name 23

24 INLINE AND OVERLOADED MEMBER FUNCTIONS Inline members Member functions defined inside the class are automatically inline Members functions defined outside the class can also be explicitly defined as inline By declaring them as inline By using inline in the function definition Overloaded member functions Member functions can be overloaded as long as the functions differ by the number and/or type of parameters Dr. Juan J. Durillo 24

25 FUNCTIONS THAT RETURN *THIS A member function may return the object to which the function is applied A sequence of actions into a single expression can then execute on the same object myclass.setvalues(4,0).setvalue( # ) ; setvalues and setvalue have to be defined as returning MyClass & Have setvalues returned a non-reference type, the call to setvalue would have been done in a temporary copy instead of the object itself Dr. Juan J. Durillo 25

26 OVERLOADING BASED ON CONST We can overload a member function based on whether it is const The non const version won t be available for non const objects The const member functions will only apply to const objects Dr. Juan J. Durillo 26

27 CLASS DECLARATION A class can be declared without defining it class MyClass; // class is declared A declared but undefined class introduces the name of that class as a Type It can be used to define pointers or references to such types and to declare (not define) classes returning or using that type A class must be however defined (not only declared) before writing code that uses that class, and also before a reference or pointer is used to access to that class Dr. Juan J. Durillo 27

28 CLASS SCOPE From outside a class scope, data and function members may be accessed through an object, a reference, or a pointer using a member access operator Type members from a class are accessed using the scope operator MyClass::pos ht = 24, wd = 80; // use the pos type defined by MyClass MyClass scr(ht,wd, ); MyClass *p = &scr; char c = src.get(); // fetches the get member from the object src c = p->get(); // fetches the get member from the object to which p points Members defined outside a class must use the scope parameter to refer to members of that class Dr. Juan J. Durillo 28

29 NAME LOOKUP AND CLASS SCOPE Name LookUp is relatively straightforward Look for a declaration of the name within the block in which the name was used (only names declared before are considered) If the name is not found, look in the enclosing scope(s) If no declaration is found, then the program is in error status Inside member functions defined within the class First, the member declarations are compiled Function bodies are compiled only after the entire class has been seen Note This procedure is called two-phase compilation, and is the one used to compile classes Dr. Juan J. Durillo 29

30 NAME LOOKUP AND CLASS SCOPE The two-phases compilation process applies only to names used in the body of a member function Names used in declarations, return and parameter lists, must be seen before they are used If a member declaration uses a name that has not been seen yet within the class, the compiler will look for that name in the scope in which the class has been defined typedef double Money; string bal; class Account { Money balance() { return bal;} private: Money bal; }; Note Money and bal in function balance are resolve in different ways Dr. Juan J. Durillo 30

31 NAME LOOKUP AND CLASS SCOPE A inner scope can redefine a name from an outer scope even if that name has been already used in the inner scope In a class, if a member uses a name from an outer scope and that name is a type, then the class must not subsequently define the name typedef double Money; class Account { public: Money balance() { return bal;} // uses Money from the outer scope private: typedef long long Money; // error cannot redefine Money!!! }; Note Remember that definition of type names usually should appear at the beginning of a class. That way nay member that uses that type will be seen after the type name has already been defined Dr. Juan J. Durillo 31

32 NAME LOOKUP AND CLASS SCOPE Names used in the body of member functions are resolved as follows Look for a declaration of the name inside the member function (as usual, only declarations in the function body that precede the use of the name are considered) If the declaration is not found before within the member function, look for a declaration inside the class All the member of the class are considered If a declaration for the name is not found in the class, look for a declaration that is in scope before the member function definition Dr. Juan J. Durillo 32

33 IMPLICIT CLASS-TYPE CONVERSIONS A constructor that can be called with a single argument defines an implicit conversion to a class type Have MyClass a constructor accepting a string as single parameter a string, and a member??? combines(myclass object) we can do string null_object = whatever string ; item.combine(null_object); // automatically converts null_object to an object of MyClass Only one class-type conversion is allowed item.combine( whatever string ); // error: why is this an error? We can prevent the implicit use of a constructor by declaring the constructor explicit Constructors requiring more arguments are not used to perform an implicit conversion Dr. Juan J. Durillo 33

34 AGGREGATE CLASSES An aggregate class gives users direct access to its members and has special initialization syntax A class is aggregate if All of its data members are public It does not define any constructor It has no in-class initializers It has no base classes or virtual functions, which are class-related to object oriented Aggregate classes can be initialized by providing a brace list of member initializers They should appear in the order in declaration order of the data members Data val1 = {0, Anna }; // ok Data val2 = { Anna, 0}; // error // Example of aggregate class struct Data { int ival; string s; }; Dr. Juan J. Durillo 34

35 STATIC CLASS MEMBERS A static member is a member associated with the class rather than with individual objects declared by preceding the member the keyword static Static members can be accessed directly through the scope operator also through an object, reference or pointer of the class type Member functions can use static members directly, without the scope operator static member class are declared within the class and are usually defined outside outside we do not need to repeat the keyword static Even if you initialize a member within the class declaration, you may need to define the member outside the class Doing it always is a good programming practice Dr. Juan J. Durillo 35

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Introduction to C++ Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 20, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II C++ intro Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 26, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February 26,

More information

C++ INTERVIEW QUESTIONS

C++ INTERVIEW QUESTIONS C++ INTERVIEW QUESTIONS http://www.tutorialspoint.com/cplusplus/cpp_interview_questions.htm Copyright tutorialspoint.com Dear readers, these C++ Interview Questions have been designed specially to get

More information

www.sahajsolns.com Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

www.sahajsolns.com Chapter 4 OOPS WITH C++ Sahaj Computer Solutions Chapter 4 OOPS WITH C++ Sahaj Computer Solutions 1 Session Objectives Classes and Objects Class Declaration Class Members Data Constructors Destructors Member Functions Class Member Visibility Private,

More information

13 Classes & Objects with Constructors/Destructors

13 Classes & Objects with Constructors/Destructors 13 Classes & Objects with Constructors/Destructors 13.1 Introduction In object oriented programming, the emphasis is on data rather than function. Class is a way that binds the data & function together.

More information

Sources: On the Web: Slides will be available on:

Sources: On the Web: Slides will be available on: C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,

More information

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas CS 110B - Rule Storage Classes Page 18-1 Attributes are distinctive features of a variable. Data type, int or double for example, is an attribute. Storage class is another attribute. There are four storage

More information

Java Interview Questions and Answers

Java Interview Questions and Answers 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write and compile the java

More information

C++FA 5.1 PRACTICE MID-TERM EXAM

C++FA 5.1 PRACTICE MID-TERM EXAM C++FA 5.1 PRACTICE MID-TERM EXAM This practicemid-term exam covers sections C++FA 1.1 through C++FA 1.4 of C++ with Financial Applications by Ben Van Vliet, available at www.benvanvliet.net. 1.) A pointer

More information

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6) Semantic Analysis Scoping (Readings 7.1,7.4,7.6) Static Dynamic Parameter passing methods (7.5) Building symbol tables (7.6) How to use them to find multiply-declared and undeclared variables Type checking

More information

C++ Overloading, Constructors, Assignment operator

C++ Overloading, Constructors, Assignment operator C++ Overloading, Constructors, Assignment operator 1 Overloading Before looking at the initialization of objects in C++ with constructors, we need to understand what function overloading is In C, two functions

More information

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c Lecture 3 Data structures arrays structs C strings: array of chars Arrays as parameters to functions Multiple subscripted arrays Structs as parameters to functions Default arguments Inline functions Redirection

More information

Object Oriented Software Design

Object Oriented Software Design Object Oriented Software Design Introduction to Java - II Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 28, 2010 G. Lipari (Scuola Superiore Sant Anna) Introduction

More information

Moving from CS 61A Scheme to CS 61B Java

Moving from CS 61A Scheme to CS 61B Java Moving from CS 61A Scheme to CS 61B Java Introduction Java is an object-oriented language. This document describes some of the differences between object-oriented programming in Scheme (which we hope you

More information

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship. CSCI 253 Object Oriented Design: Java Review OOP George Blankenship George Blankenship 1 Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation Abstract Data Type (ADT) Implementation

More information

Object Oriented Software Design

Object Oriented Software Design Object Oriented Software Design Introduction to Java - II Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa September 14, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction

More information

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1 Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1 Introduction to Classes Classes as user-defined types We have seen that C++ provides a fairly large set of built-in types. e.g

More information

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1 The Java Series Java Essentials I What is Java? Basic Language Constructs Slide 1 What is Java? A general purpose Object Oriented programming language. Created by Sun Microsystems. It s a general purpose

More information

Conversion Constructors

Conversion Constructors CS106L Winter 2007-2008 Handout #18 Wednesday, February 27 Conversion Constructors Introduction When designing classes, you might find that certain data types can logically be converted into objects of

More information

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator= CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator= We already know that the compiler will supply a default (zero-argument) constructor if the programmer does not specify one.

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Real Application Design Christian Nastasi http://retis.sssup.it/~lipari http://retis.sssup.it/~chris/cpp Scuola Superiore Sant Anna Pisa April 25, 2012 C. Nastasi (Scuola

More information

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007 Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007 The Java Type System By now, you have seen a fair amount of Java. Time to study in more depth the foundations of the language,

More information

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C 1 An essential part of any embedded system design Programming 2 Programming in Assembly or HLL Processor and memory-sensitive

More information

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is preceded by an equal sign d. its name has undereline 2. Associations

More information

Applied Informatics C++ Coding Style Guide

Applied Informatics C++ Coding Style Guide C++ Coding Style Guide Rules and Recommendations Version 1.4 Purpose of This Document This document describes the C++ coding style employed by Applied Informatics. The document is targeted at developers

More information

Chapter 13 - Inheritance

Chapter 13 - Inheritance Goals Chapter 13 - Inheritance To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn about protected and package

More information

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Scope Scope and Lifetime Referencing Environments Named

More information

Getting Started with the Internet Communications Engine

Getting Started with the Internet Communications Engine Getting Started with the Internet Communications Engine David Vriezen April 7, 2014 Contents 1 Introduction 2 2 About Ice 2 2.1 Proxies................................. 2 3 Setting Up ICE 2 4 Slices 2

More information

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement? 1. Distinguish & and && operators. PART-A Questions 2. How does an enumerated statement differ from a typedef statement? 3. What are the various members of a class? 4. Who can access the protected members

More information

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes Cicles Formatius de Grau Superior Desenvolupament d Aplicacions Informàtiques D06 PROGRAMMING with JAVA Ch3 Implementing Classes PowerPoint presentation, created by Angel A. Juan - ajuanp(@)gmail.com,

More information

Install Java Development Kit (JDK) 1.8 http://www.oracle.com/technetwork/java/javase/downloads/index.html

Install Java Development Kit (JDK) 1.8 http://www.oracle.com/technetwork/java/javase/downloads/index.html CS 259: Data Structures with Java Hello World with the IntelliJ IDE Instructor: Joel Castellanos e-mail: joel.unm.edu Web: http://cs.unm.edu/~joel/ Office: Farris Engineering Center 319 8/19/2015 Install

More information

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following: In creating objects of the type, we have used statements similar to the following: f = new (); The parentheses in the expression () makes it look like a method, yet we never created such a method in our

More information

Tutorial on C Language Programming

Tutorial on C Language Programming Tutorial on C Language Programming Teodor Rus rus@cs.uiowa.edu The University of Iowa, Department of Computer Science Introduction to System Software p.1/64 Tutorial on C programming C program structure:

More information

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

KITES TECHNOLOGY COURSE MODULE (C, C++, DS) KITES TECHNOLOGY 360 Degree Solution www.kitestechnology.com/academy.php info@kitestechnology.com technologykites@gmail.com Contact: - 8961334776 9433759247 9830639522.NET JAVA WEB DESIGN PHP SQL, PL/SQL

More information

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved.

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved. 1 2 Introduction to Java Applications 2.2 First Program in Java: Printing a Line of Text 2 Application Executes when you use the java command to launch the Java Virtual Machine (JVM) Sample program Displays

More information

C++ Programming Language

C++ Programming Language C++ Programming Language Lecturer: Yuri Nefedov 7th and 8th semesters Lectures: 34 hours (7th semester); 32 hours (8th semester). Seminars: 34 hours (7th semester); 32 hours (8th semester). Course abstract

More information

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go Debugging ESE112 Java Programming: API, Psuedo-Code, Scope It is highly unlikely that you will write code that will work on the first go Bugs or errors Syntax Fixable if you learn to read compiler error

More information

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance Introduction to C++ January 19, 2011 Massachusetts Institute of Technology 6.096 Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance We ve already seen how to define composite datatypes

More information

A deeper look at Inline functions

A deeper look at Inline functions A deeper look at Inline functions I think it s safe to say that all Overload readers know what C++ inline functions are. When we declare a function or member function as inline we are trying to avoid the

More information

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example Operator Overloading Lecture 8 Operator Overloading C++ feature that allows implementer-defined classes to specify class-specific function for operators Benefits allows classes to provide natural semantics

More information

Java (12 Weeks) Introduction to Java Programming Language

Java (12 Weeks) Introduction to Java Programming Language Java (12 Weeks) Topic Lecture No. Introduction to Java Programming Language 1 An Introduction to Java o Java as a Programming Platform, The Java "White Paper" Buzzwords, Java and the Internet, A Short

More information

Programming Database lectures for mathema

Programming Database lectures for mathema Programming Database lectures for mathematics students April 25, 2015 Functions Functions are defined in Postgres with CREATE FUNCTION name(parameter type,...) RETURNS result-type AS $$ function-body $$

More information

Chapter 13 Storage classes

Chapter 13 Storage classes Chapter 13 Storage classes 1. Storage classes 2. Storage Class auto 3. Storage Class extern 4. Storage Class static 5. Storage Class register 6. Global and Local Variables 7. Nested Blocks with the Same

More information

LAB4 Making Classes and Objects

LAB4 Making Classes and Objects LAB4 Making Classes and Objects Objective The main objective of this lab is class creation, how its constructer creation, object creation and instantiation of objects. We will use the definition pane to

More information

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5 This Week CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5 School of Computer Science University of St Andrews Graham Kirby Alan Dearle More on Java classes Constructors Modifiers cdn.videogum.com/img/thumbnails/photos/commenter.jpg

More information

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program. Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to

More information

Glossary of Object Oriented Terms

Glossary of Object Oriented Terms Appendix E Glossary of Object Oriented Terms abstract class: A class primarily intended to define an instance, but can not be instantiated without additional methods. abstract data type: An abstraction

More information

Delegating Constructors (revision 3)

Delegating Constructors (revision 3) Doc No: SC22/WG21/N1986 J16/06 0056 Date: 2006 04 06 Project: JTC1.22.32 Reply to: Herb Sutter Francis Glassborow Microsoft Corp. Association of C & C++ Users 1 Microsoft Way 64 Southfield Road Redmond

More information

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies) Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies) Duration of Course: 6 Months Fees: Rs. 25,000/- (including Service Tax) Eligibility: B.E./B.Tech., M.Sc.(IT/ computer

More information

Semantic Analysis: Types and Type Checking

Semantic Analysis: Types and Type Checking Semantic Analysis Semantic Analysis: Types and Type Checking CS 471 October 10, 2007 Source code Lexical Analysis tokens Syntactic Analysis AST Semantic Analysis AST Intermediate Code Gen lexical errors

More information

Goals for This Lecture:

Goals for This Lecture: Goals for This Lecture: Understand the pass-by-value and passby-reference argument passing mechanisms of C++ Understand the use of C++ arrays Understand how arrays are passed to C++ functions Call-by-value

More information

C++FA 3.1 OPTIMIZING C++

C++FA 3.1 OPTIMIZING C++ C++FA 3.1 OPTIMIZING C++ Ben Van Vliet Measuring Performance Performance can be measured and judged in different ways execution time, memory usage, error count, ease of use and trade offs usually have

More information

Java from a C perspective. Plan

Java from a C perspective. Plan Java from a C perspective Cristian Bogdan 2D2052/ingint04 Plan Objectives and Book Packages and Classes Types and memory allocation Syntax and C-like Statements Object Orientation (minimal intro) Exceptions,

More information

Lecture 2 Notes: Flow of Control

Lecture 2 Notes: Flow of Control 6.096 Introduction to C++ January, 2011 Massachusetts Institute of Technology John Marrero Lecture 2 Notes: Flow of Control 1 Motivation Normally, a program executes statements from first to last. The

More information

Functions and Parameter Passing

Functions and Parameter Passing Chapter 5: Functions and Parameter Passing In this chapter, we examine the difference between function calls in C and C++ and the resulting difference in the way functions are defined in the two languages.

More information

OpenCL Static C++ Kernel Language Extension

OpenCL Static C++ Kernel Language Extension OpenCL Static C++ Kernel Language Extension Document Revision: 04 Advanced Micro Devices Authors: Ofer Rosenberg, Benedict R. Gaster, Bixia Zheng, Irina Lipov December 15, 2011 Contents 1 Overview... 3

More information

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science First Java Programs V. Paúl Pauca Department of Computer Science Wake Forest University CSC 111D Fall, 2015 Hello World revisited / 8/23/15 The f i r s t o b l i g a t o r y Java program @author Paul Pauca

More information

Object-Oriented Programming Lecture 2: Classes and Objects

Object-Oriented Programming Lecture 2: Classes and Objects Object-Oriented Programming Lecture 2: Classes and Objects Dr. Lê H!ng Ph"#ng -- Department of Mathematics, Mechanics and Informatics, VNUH July 2012 1 Content Class Object More on class Enum types Package

More information

1 Abstract Data Types Information Hiding

1 Abstract Data Types Information Hiding 1 1 Abstract Data Types Information Hiding 1.1 Data Types Data types are an integral part of every programming language. ANSI-C has int, double and char to name just a few. Programmers are rarely content

More information

Object Oriented Programming With C++(10CS36) Question Bank. UNIT 1: Introduction to C++

Object Oriented Programming With C++(10CS36) Question Bank. UNIT 1: Introduction to C++ Question Bank UNIT 1: Introduction to C++ 1. What is Procedure-oriented Programming System? Dec 2005 2. What is Object-oriented Programming System? June 2006 3. Explain the console I/O functions supported

More information

PL / SQL Basics. Chapter 3

PL / SQL Basics. Chapter 3 PL / SQL Basics Chapter 3 PL / SQL Basics PL / SQL block Lexical units Variable declarations PL / SQL types Expressions and operators PL / SQL control structures PL / SQL style guide 2 PL / SQL Block Basic

More information

Variable Base Interface

Variable Base Interface Chapter 6 Variable Base Interface 6.1 Introduction Finite element codes has been changed a lot during the evolution of the Finite Element Method, In its early times, finite element applications were developed

More information

arrays C Programming Language - Arrays

arrays C Programming Language - Arrays arrays So far, we have been using only scalar variables scalar meaning a variable with a single value But many things require a set of related values coordinates or vectors require 3 (or 2, or 4, or more)

More information

Friendship and Encapsulation in C++

Friendship and Encapsulation in C++ Friendship and Encapsulation in C++ Adrian P Robson Department of Computing University of Northumbria at Newcastle 23rd October 1995 Abstract There is much confusion and debate about friendship and encapsulation

More information

17. Friendship and Inheritance

17. Friendship and Inheritance - 117 - Object Oriented Programming: 17. Friendship and Inheritance Friend functions In principle, private and protected members of a class cannot be accessed from outside the same class in which they

More information

The different kinds of variables in a Java program

The different kinds of variables in a Java program The different kinds of variables in a Java program The different kinds of variables in a Java program Java has 4 different kinds of variables Class variables Instance variables Local variables Parameter

More information

Chapter One Introduction to Programming

Chapter One Introduction to Programming Chapter One Introduction to Programming 1-1 Algorithm and Flowchart Algorithm is a step-by-step procedure for calculation. More precisely, algorithm is an effective method expressed as a finite list of

More information

VB.NET Programming Fundamentals

VB.NET Programming Fundamentals Chapter 3 Objectives Programming Fundamentals In this chapter, you will: Learn about the programming language Write a module definition Use variables and data types Compute with Write decision-making statements

More information

GENERIC and GIMPLE: A New Tree Representation for Entire Functions

GENERIC and GIMPLE: A New Tree Representation for Entire Functions GENERIC and GIMPLE: A New Tree Representation for Entire Functions Jason Merrill Red Hat, Inc. jason@redhat.com 1 Abstract The tree SSA project requires a tree representation of functions for the optimizers

More information

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc. WA2099 Introduction to Java using RAD 8.0 Student Labs Web Age Solutions Inc. 1 Table of Contents Lab 1 - The HelloWorld Class...3 Lab 2 - Refining The HelloWorld Class...20 Lab 3 - The Arithmetic Class...25

More information

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Introduction to Object-Oriented Programming Programs and Methods Christopher Simpkins chris.simpkins@gatech.edu CS 1331 (Georgia Tech) Programs and Methods 1 / 8 The Anatomy of a Java Program It is customary

More information

Software Development (CS2500)

Software Development (CS2500) (CS2500) Lecture 15: JavaDoc and November 6, 2009 Outline Today we study: The documentation mechanism. Some important Java coding conventions. From now on you should use and make your code comply to the

More information

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer Lecture 10: Static Semantics Overview 1 Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing Produces trees Detects & eliminates ill-formed parse trees Static semantic analysis

More information

More C++ Concepts. Operator overloading Friend Function This Operator Inline Function

More C++ Concepts. Operator overloading Friend Function This Operator Inline Function More C++ Concepts Operator overloading Friend Function This Operator Inline Function 1 Review There are different types of member functions in the definition of a class Accessor int Str :: get_length();

More information

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON PROBLEM SOLVING WITH SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON Addison Wesley Boston San Francisco New York London

More information

Database Programming with PL/SQL: Learning Objectives

Database Programming with PL/SQL: Learning Objectives Database Programming with PL/SQL: Learning Objectives This course covers PL/SQL, a procedural language extension to SQL. Through an innovative project-based approach, students learn procedural logic constructs

More information

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void 1. Explain C tokens Tokens are basic building blocks of a C program. A token is the smallest element of a C program that is meaningful to the compiler. The C compiler recognizes the following kinds of

More information

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint) TN203 Porting a Program to Dynamic C Introduction Dynamic C has a number of improvements and differences compared to many other C compiler systems. This application note gives instructions and suggestions

More information

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below: http://www.tutorialspoint.com/java/java_methods.htm JAVA - METHODS Copyright tutorialspoint.com A Java method is a collection of statements that are grouped together to perform an operation. When you call

More information

6. Control Structures

6. Control Structures - 35 - Control Structures: 6. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose,

More information

1. Polymorphism in C++...2

1. Polymorphism in C++...2 1. Polymorphism in C++...2 1.1 Polymorphism and virtual functions... 2 1.2 Function call binding... 3 1.3 Virtual functions... 4 1.4 How C++ implements late binding... 6 1.4.1 Why do I have to know at

More information

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Handout 1 CS603 Object-Oriented Programming Fall 15 Page 1 of 11 Handout 1 Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Java

More information

COMP 110 Prasun Dewan 1

COMP 110 Prasun Dewan 1 COMP 110 Prasun Dewan 1 12. Conditionals Real-life algorithms seldom do the same thing each time they are executed. For instance, our plan for studying this chapter may be to read it in the park, if it

More information

Introduction to Data Structures

Introduction to Data Structures Introduction to Data Structures Albert Gural October 28, 2011 1 Introduction When trying to convert from an algorithm to the actual code, one important aspect to consider is how to store and manipulate

More information

Contents. 9-1 Copyright (c) 1999-2004 N. Afshartous

Contents. 9-1 Copyright (c) 1999-2004 N. Afshartous Contents 1. Introduction 2. Types and Variables 3. Statements and Control Flow 4. Reading Input 5. Classes and Objects 6. Arrays 7. Methods 8. Scope and Lifetime 9. Utility classes 10. Introduction to

More information

MISRA-C:2012 Standards Model Summary for C / C++

MISRA-C:2012 Standards Model Summary for C / C++ MISRA-C:2012 Standards Model Summary for C / C++ The LDRA tool suite is developed and certified to BS EN ISO 9001:2000. This information is applicable to version 9.4.2 of the LDRA tool suite. It is correct

More information

Introduction to Objective-C. Kevin Cathey

Introduction to Objective-C. Kevin Cathey Introduction to Objective-C Kevin Cathey Introduction to Objective-C What are object-oriented systems? What is the Objective-C language? What are objects? How do you create classes in Objective-C? acm.uiuc.edu/macwarriors/devphone

More information

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) { Operators C and C++ 6. Operators Inheritance Virtual Alastair R. Beresford University of Cambridge Lent Term 2008 C++ allows the programmer to overload the built-in operators For example, a new test for

More information

Symbol Tables. Introduction

Symbol Tables. Introduction Symbol Tables Introduction A compiler needs to collect and use information about the names appearing in the source program. This information is entered into a data structure called a symbol table. The

More information

Parameter passing in LISP

Parameter passing in LISP Parameter passing in LISP The actual parameters in a function call are always expressions, represented as lists structures. LISP provides two main methods of parameter passing: Pass/Call-by-value. The

More information

Computer Programming C++ Classes and Objects 15 th Lecture

Computer Programming C++ Classes and Objects 15 th Lecture Computer Programming C++ Classes and Objects 15 th Lecture 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University Copyrights 2013 Eom, Hyeonsang All Rights Reserved Outline

More information

CORBA Programming with TAOX11. The C++11 CORBA Implementation

CORBA Programming with TAOX11. The C++11 CORBA Implementation CORBA Programming with TAOX11 The C++11 CORBA Implementation TAOX11: the CORBA Implementation by Remedy IT TAOX11 simplifies development of CORBA based applications IDL to C++11 language mapping is easy

More information

Basic Object-Oriented Programming in Java

Basic Object-Oriented Programming in Java core programming Basic Object-Oriented Programming in Java 1 2001-2003 Marty Hall, Larry Brown http:// Agenda Similarities and differences between Java and C++ Object-oriented nomenclature and conventions

More information

Concepts and terminology in the Simula Programming Language

Concepts and terminology in the Simula Programming Language Concepts and terminology in the Simula Programming Language An introduction for new readers of Simula literature Stein Krogdahl Department of Informatics University of Oslo, Norway April 2010 Introduction

More information

Fundamentals of Java Programming

Fundamentals of Java Programming Fundamentals of Java Programming This document is exclusive property of Cisco Systems, Inc. Permission is granted to print and copy this document for non-commercial distribution and exclusive use by instructors

More information

Stack Allocation. Run-Time Data Structures. Static Structures

Stack Allocation. Run-Time Data Structures. Static Structures Run-Time Data Structures Stack Allocation Static Structures For static structures, a fixed address is used throughout execution. This is the oldest and simplest memory organization. In current compilers,

More information

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013 Masters programmes in Computer Science and Information Systems Object-Oriented Design and Programming Sample module entry test xxth December 2013 This sample paper has more questions than the real paper

More information

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C Parameter Passing In this set of notes you will learn about: Parameter passing modes Call by Call by reference Call by sharing Call by result Call by /result Call by name Subroutine closures as parameters

More information

A Grammar for the C- Programming Language (Version S16) March 12, 2016

A Grammar for the C- Programming Language (Version S16) March 12, 2016 A Grammar for the C- Programming Language (Version S16) 1 Introduction March 12, 2016 This is a grammar for this semester s C- programming language. This language is very similar to C and has a lot of

More information

Binary compatibility for library developers. Thiago Macieira, Qt Core Maintainer LinuxCon North America, New Orleans, Sept. 2013

Binary compatibility for library developers. Thiago Macieira, Qt Core Maintainer LinuxCon North America, New Orleans, Sept. 2013 Binary compatibility for library developers Thiago Macieira, Qt Core Maintainer LinuxCon North America, New Orleans, Sept. 2013 Who am I? Open Source developer for 15 years C++ developer for 13 years Software

More information