1 from java to c
Download
1 / 14

1 . From Java to C++ - PowerPoint PPT Presentation


  • 58 Views
  • Uploaded on

1 . From Java to C++. Yan Shi CS/SE 2630 Lecture Notes. C++ Review. if, while, for: same in Java and C++ 4 parts of a loop? EOF loop: [ctrl-z] in Visual Studio simple C++ data types: int , long, short, float, double, bool , enum cin , cout : need to #include< iostream >

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about '1 . From Java to C++' - rio


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
1 from java to c

1. From Java to C++

Yan Shi

CS/SE 2630 Lecture Notes


C review
C++ Review

  • if, while, for:

    • same in Java and C++

    • 4 parts of a loop?

    • EOF loop: [ctrl-z] in Visual Studio

  • simple C++ data types:

    • int, long, short, float, double, bool, enum

  • cin, cout: need to #include<iostream>

  • Array: intnums[500];

  • Reference parameter: for arrays and class objects

  • String operations:

    • #include <string> OR

    • #include <cstring>


C class review
C++ Class Review

  • Write the IntList class (IntList0.cpp):

    • Add, Delete


What is different from java
What is different from JAVA?

  • semicolon at the end of a class declaration

  • use private: and public: to group data fields and methods

  • create a IntList object: IntList list;

    • There are other ways to do that. We will discuss later.

    • In java: IntList list = new IntList();

  • main function: outside the class declaration

  • use cout instead of System.out.println()

  • use cin to read from stdin – much easier than Java!

  • Don’t declare a class as public or private!

    • How do we declare a public or private class then?


Abstract data type adt
Abstract Data Type (ADT)

  • ADT: a data type whose properties are specified independently of any particular implementation.

    • Abstraction, Encapsulation, Information hiding

  • In C++, we use two files for each class:

    • *.h: header file, class declaration, specification

    • *.cpp: implementation file, class definition

      In this way, the class is by default public!

      If you put everything in one .cpp file without a .h file (like in our previous example), it is a private class.


Example
Example

Rewrite IntList class into IntList.h and IntList.cpp files

  • remove implementations from class definition  IntList.h

    • Note: method documentation belongs in .h file

    • You can leave on-liner methods in .h file

  • define methods in IntList.cpp file

    • What if there are multiple Add functions in the project?

    • Solution: use scope resolution operator ::

    • boolIntList::Add( intnewNum){…}

  • move constintMAX_SIZE;into class IntList: global scope is bad!

    • have to change to static constintMAX_SIZE;

  • move the main function into a separate client file IntListClient.cpp

    • try not to use any I/O in domain classes: that is against the OO design principle!

    • create a driver/client to utilize domain classes.



Running a program

Lab0.cpp

Lab0.obj

Lab0.exe

SOURCE

OBJECT

EXECUTABLE

written in

C++

written in

machine

language

written in

machine

language

via linker

via compiler

other code

from libraries,

etc.

Running a program

  • In CS1430, we introduced the compilation process:

more details


C preprocessor1
C++ Preprocessor

  • handle preprocessor directives

    • macro definitions: #define, #undef

    • conditional inclusions: #ifdef, #ifndef, #endif, #else and #elif

    • source file inclusion: #include

  • remove comments

.h

files

compilation

unit(.i)

.cpp

files


Source file inclusion include
Source File Inclusion: #include

  • Example:

    • #include <iostream>

    • #include “IntList.h”

  • The preprocessor will replace the directive by given file (iostream and IntList.h)

    • If you have function definitions in the header file, that means they might be copied multiple times if the header file is included in multiple places!  save time at the cost of space!

  • Difference between <file> and “file”:

    • #include <file> looks for file in standard directories

    • #include “file” looks for file in the current directory. That is, file is part of the project!

  • Never use #include to include a .cppfile in another!


Prevent multiple inclusion
Prevent Multiple Inclusion

  • What if the file is included more than once?

    • suppose we have Student and StudentList class. In the StudentSystem.cpp file (client), we include both “Student.h” and “StudentList.h”.

  • Use the inclusion guard!

    #ifndef _INTLIST_H

    #define _INTLIST_H

    regular IntList.h code goes here…

    #endif

  • An alternative: #pragma once

    • less code, avoid name clashes

    • non-standard, may cause portability issue


Macro definitions
Macro Definitions

#define identifier replacement

  • When the preprocessor encounters this directive, it replaces any occurrence of identifier in the rest of the code by replacement until #undefis met.

    • It can be used as a constant definer: #define MAX 9

  • This replacement can be an expression, a statement, a block or simply anything. But it should be on the same line.

    Try to avoid using macros if possible: code that relies heavily on complicated macros may seem obscure to other programmers!


Conditional inclusions
Conditional Inclusions

  • #ifdef, #ifndef, #if, #endif, #else and #elif

  • These directives allow to include or discard part of the code of a program if a certain condition is met.

  • Examples:

#ifdef SIZE

intlist[SIZE];

#endif

only compiled if SIZE was previously

defined with #define

#if SIZE<10

#undef SIZE

#define SIZE 10

#elifSIZE>100

#undef SIZE

#define SIZE 100

#else

#undef SIZE

#define SIZE 50

#endif

int list[SIZE];

The condition that follows #if or #elif can only evaluate

constant expressions, including macro expressions.


Stack queue review
Stack & Queue Review

  • Stack is LIFO.

    • Basic operations: IsEmpty(), IsFull(), Pop(), Push(Item), Top()

  • Queue is FIFO.

    • Basic operations: IsEmpty(), IsFull(), Enqueue(Item), Dequeue(Item&)

      Can we use array to implement them? How?