User Tools

Site Tools


opus:spring2012:smalik2:cprogpart2

cprog Keywords

9. Type Casting

Definition

Type casting is basically telling the program to treat a certain entity (an int, char, whatever) using the rules of a different one.

Demonstration

So if we use…

int a = 3; int b = 5;

int c = a/b;

c is actually zero, because of integer division. But if we use this line instead….

int c; (float)c = a/b;

The (float) is a type cast to c, telling the program to treat c as a float, which would ensure that it stores a 0.6 instead of a 0.

10. I/O Streams (cin, cout, cerr, stream operators) [C++]

Definition

Needs #include <iostream>

cout produces output ~ printf cin provides input ~ scanf cerr is just the error output stream operators are things such as setf() and setw() which held you do different things with the stream.

Demonstration

int a=1 int b=2

printf(“a is %d, b is %d\n”, a, b); cout « “a is ” « a « “,b is ” « b « endl;

scanf(“%d”, %a); cin » a;

cerr « “Error, yo” « endl;

Stream operator in use: cout « setw(5) « a « “ sheep are sleeping.” « endl;

11. Header Files (Local and System), C Standard Library (Libc), Libraries

Definition

Header files are basically files that contain class and function 'prototypes'. It basically will let the program know that there is a class or function which is named something, and the types of parameters and return type it has. Local Header files are the ones that tend to be user made, or could get pulled in from another source I suppose. System header files are the ones that are in the system.

The C Standard Library has a bunch of the most common and useful C header files I would guess, such as stdio for input and output capabilities.

Libraries in C contain all of these functions and whatnot in them.

Demonstration

To call a system header

#include <stdio>

To call a local header

#include “nor.h”

12. Arithmetic (equations, operators)

Definition

Arithmetic is the way that we can manipulate data. Equations would set some value equal to something else, while operators can compare values.

Demonstration

To assign a value to a variable using an equation:

Density = 50*8/13;

To compare using an operator:

if (Density < 100)
{
    printf("Wow")
}

13. Scope (Block, Local, Global, File)

Definition

Scope is basically the region of the program in which a declared variable will exist. Block would mean the scope is in regards to the set of brackets you're in. Local would mean within a function. Global would mean within an entire file of code. File would (I assume) mean between files. Although how to do this I do not know.

Demonstration

if( x>3)
{
    int Q = 5;   // Q only exists within this if statement
}
 
int Sum1(int num1, int num2)   //these two integers are only defined within this function!
{
    return (num1 + num2)
}
 
int x;     //this variable will be defined throughout the entire file, regardless of functions and whatnot.
int main()
{
    x = 4
}

14. Functions, Parameters (Pass by: Value, Address, Reference), Return Types, Recursion, Command-line arguments

Definition

Functions are basically isolate segments of code that can do certain things based on Parameters that you pass them. It saves a lot of coding, for example, to have a function that you just send different values of length and width to calculate the area of 50 squares is much easier than actually writing the code to do 50 different areas.
Return Types are the variable type that the function will be returning- in the case of the area function, we would probably want to return a float value.
Recursion is basically when a function calls itself within itself, with some end terminating condition. We haven't covered it in class yet though.
Command-line arguments are when you pass parameters to the main function of the program.

Demonstration

There are some great examples of functions in Project 1 and Project 2. Everything is used except Recursion.

15. Overloading (Functions, Operators) [C++]

Definition

Overloading a function is to use the same function name for multiple functions, with the difference being the parameters passed to the function.

Demonstration

Let's say we want to write a program that will calculate areas. Rectangles, Squares, and Circles. We'll overload a function to do this.

int Area(int);    //for the square
float Area(float, float);   //for the rectangle
float Area(float);    //for the circle

These functions will actually work, and depending on the parameters that we send, it will either call the one meant for squares, rectangles, or circles!

16. Inheritance (single, multiple Inheritance), Polymorphism/Virtual Functions, Abstract Base Class [C++]

Definition

Inheritance is how classes can inherit the stuff from the public and protected part of their 'parent' class. Multiple Inheritance is when there are multiple layers going on.

Demonstration

To see an excellent example of inheritance, over multiple files, in my directory… /home/smalik2/src/cprog/c++

There is a multifile program using classes and inheritance and multiple inheritance.

cprog Objective

cprog Objective

break down and separate code into functions and illustrate the use of parameter passing

Definition

If you need to write a huge long program that does multiple things, that are similar to one another, this will be the capability to recognize the need for and be able to split the program apart into functions, so that code repetition is minimized and the code is more efficient.

Method

If I start doing what I said in the definition. Which I believe I already do fairly solidly. In Project2 we continuously printed arrays, so I actually just made a function that would print an array given the array name and the length of the array.

Measurement

I did a lot of this in Project 2.

Analysis

I just did it. There's always room for improvement, but it is not desperately needed. There's not really a way to enhance checking to see if you can do it. I think the objective is stated nicely.

opus/spring2012/smalik2/cprogpart2.txt · Last modified: 2012/04/01 20:45 by smalik2