User Tools

Site Tools


opus:spring2012:jcavalu3:cprogpart3

cprog Keywords

Compiler, Preprocessor, Flags, Assembler, Linker, Multi-file programs (how to structure, how to compile)
Namespaces [C++]
Type Casting Operators, Const-Volatility Specifiers (const, volatile) [C++]
Classes (Objects, Constructor, Destructor, Access Control, Public, Protected, Private, Friend, “this” pointer) [C++]
Inheritance (single, multiple Inheritance), Polymorphism/Virtual Functions, Abstract Base Class [C++]
Exception Handing (throw, try, catch) [C++]
Templates, STL (Standard Template Library) [C++]
Structures (Declaration, Accessing Elements, Pointers to)

cprog Keyword 17

Structures (Declaration, Accessing Elements, Pointers to)

Definition

Structures contain public variables held under one name. Each of these variables can be different data types. These variables are known as members.

Demonstration

/*
 *
 * Sample code block
 *
 */
 
#include<stdio.h>
 
int main()
{
        struct person{
                char *name;
                unsigned char age;
                short int weight;
                float gpa;
        };
 
        typedef struct person P;
        struct P p1, p2, p3, p4, p5;
 
        pl.age = 29;
        return(0);
}

cprog Keyword 18

Templates, STL (Standard Template Library) [C++]

Definition

A template is like a blueprint,

Demonstration

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    return(0);
}

Alternatively (or additionally), if you want to demonstrate something on the command-line, you can do so as follows:

lab46:~$ cd src
lab46:~/src$ gcc -o hello hello.c
lab46:~/src$ ./hello
Hello, World!
lab46:~/src$ 

cprog Keyword 19

Classes (Objects, Constructor, Destructor, Access Control, Public, Protected, Private, Friend, “this” pointer) [C++]

Definition

A class is similar to a struct. The main difference is that a class can hold both data and functions. Objects are created by using classes. Access control includes the use of the private, public, and protected keywords. They actually determine what can access the variables in the class. Public allows anyone to access those variables, private allows only the members of the same class to access those variables, and protected allows only members of the same class and their friends.

Demonstration

Demonstration of the chosen keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

/*
 * Sample code block
 */
#include <cstdio>
#include "num.h"
 
        class num {
                public:
                        num();
//                      ~num();
                        float average(int, int, int, int);
//                      int sum(int, int, int, int);
                        void set(int,int,int,int);
                        int get(int);
                private:
                        int n1,n2,n3,n4;
                  };
 
        num :: num()
        {
                n1=0;
                n2=0;
                n3=0;
                n4=0;
        }
 
        float num :: average(int a, int b, int c, int d)
        {
                return((float)(a+b+c+d)/4);
        }
 
 
        void num :: set(int a, int b, int c, int d)
        {
                n1=a;
                n2=b;
                n3=c;
                n4=d;
        }
 
        int num :: get(int value)
        {
                int result=0;
                switch(value)
                {
                        case 1:
                                result=n1;
                                break;
                        case 2:
                                result=n2;
                                break;
                        case 3:
                                result=n3;
                                break;
                        case 4:
                                result=n4;
                                break;
                        default:
                                result=-9999;
                                break;
                }
                return(result);
        }
 
 
int main()
{
        num myNum;
        myNum.set(7,0,0,0);
        return(0);
}

cprog Keyword 20

Exception Handling (throw, try, catch) [C++]

Definition

Exceptions, from what I have seen, appear to be a type of debugging that can occur during runtime. The program will run through a try block, and if the exception does not exist, then the program will continue running.

Demonstration

Demonstration of the chosen keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    return(0);
}

Alternatively (or additionally), if you want to demonstrate something on the command-line, you can do so as follows:

lab46:~$ cd src
lab46:~/src$ gcc -o hello hello.c
lab46:~/src$ ./hello
Hello, World!
lab46:~/src$ 

cprog Keyword 21

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

Definition

Inheritance allows a program to reuse existing classes in different cases, without actually changing the values in the classes.

Demonstration

Demonstration of the chosen keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

The AND file:

#include "gate.h"
#include "and.h"
 
AND::AND()
{
        setA(false);
        setB(false);
        setX(false);
}
 
void AND::process()
{
        if((A==true) and (B==true))
                setX(true);
        else
                setX(false);
}

The NOT file:

#include "gate.h"
#include "not.h"
 
NOT::NOT()
{
        setA(false);
        setX(false);
}
 
void NOT::process()
{
        if(A == false)
                setX(true);
        else
                setX(false);
}

The OR file:

#include "gate.h"
#include "or.h"
 
OR::OR()
{
        setA(false);
        setB(false);
        setX(false);
}
 
void OR::process()
{
        if((A == false) and (B == false))
                setX(false);
        else
                setX(true);
}

The main file:

#include"gate.h"
#include"and.h"
#include"or.h"
#include"not.h"
#include<stdio.h>
 
int main()
{
        AND *myAND = new AND();
        myAND -> setA(true);
        myAND -> setB(false);
        myAND -> process();
        printf("Result of AND is: %d\n", myAND->getX());
 
        OR *yoOR = new OR();
        yoOR -> setA( true );
        yoOR -> setB( true );
        yoOR -> process();
        printf("Result of OR is: %d\n", yoOR -> getX());
 
        NOT *whyNOT = new NOT();
        whyNOT -> setA( false );
        whyNOT -> process();
        printf("Not A is: %d\n", whyNOT -> getX());
        return(0);
}

With the following header files, the C++ files can use the same class file:

The OR header file:

#ifndef _OR_H
#define _OR_H
 
class OR:public gate {
        public:
                OR();
                void process();
};
 
#endif

The AND header file:

#ifndef _AND_H
#define _AND_H
 
class AND:public gate {
        public:
                AND();
                void process();
};
 
#endif

The NOT header file:

#ifndef _NOT_H
#define _NOT_H
 
class NOT:public gate {
        public:
                NOT();
                void process();
};
 
#endif

The most important files of the code, the ones that hold the classes to allow inheritance, the gate.cc file and the gate.h file:

#include "gate.h"
 
gate :: gate()
{
        A=B=X=false;
}
 
//returns X
bool gate :: getX()
{
        return(X);
}
 
void gate :: setA(bool A)
{
        this->A=A; // 'this.' allows you to reference yourself.'this.A' is
                  // the A in the protected part of the class.
}
 
void gate :: setB(bool B)
{
        this->B=B;
}
 
void gate :: setX(bool X)
{
        this->X=X;
}
#ifndef _GATE_H
#define _GATE_H
 
class gate {
        public:
                gate();
                bool getX();// LOOK UP BOOL DATA TYPE!!!!
                void setA(bool);
                void setB(bool);
        protected:
                void setX(bool);
                bool A;
                bool B;
        private:
                bool X;
};
 
#endif

cprog Keyword 22

Compiler, Preprocessor, Flags, Assembler, Linker, Multi-file programs (how to structure, how to compile)

Definition

The compiler turns source code into executable code. The preprocessor takes data that is input into a compiler and returns output that can be used in another program. Multi-file programs take separate files and, when compiled together, take data from each file and run a program.

Demonstration

 

cprog Keyword 23

Namespaces [C++]

Definition

Namespaces allow programs to group classes and objects together and be used under different names.

Demonstration

Demonstration of the chosen keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    return(0);
}

Alternatively (or additionally), if you want to demonstrate something on the command-line, you can do so as follows:

lab46:~$ cd src
lab46:~/src$ gcc -o hello hello.c
lab46:~/src$ ./hello
Hello, World!
lab46:~/src$ 

cprog Keyword 24

Type Casting Operators, Const-Volatility Specifiers (const, volatile) [C++]

Definition

Type casting allows a program to assign a variable of one data type equal to a variable of another data type, and then forces that value to be the new data type. Type casting operators consist of dynamic_cast, static_cast, reinterpret_cast, const_cast, and typeid. Const-Volatility Specifiers set types as whichever you choose. Const sets the type to const, and volatile set the type to volatile.

Demonstration

Demonstration:

 

cprog Objective

cprog Objective

Distinguish and explain difference between homogeneous and heterogeneous composite data types:

Definition

Homogeneous composite data types consist of created variables with multiple spaces of memory that can either be empty or filled, that are of the same data type. Heterogeneous composite data types consist of very similar variables, except they hold multiple data types.

Method

Some of the various data types that are capable of holding more than one value or space of memory are:

  • struct
  • arrays
  • classes
  • unions

What determines whether or not they are heterogeneous or homogeneous is whether or not they have more than one data type stored in them.

Measurement

Most arrays are of one data type, which is defined when the array is initialized:

   char *array[10];

Unions and Structs are very similar, and they both can have more than one data type. They are seen more often as the group of code that would have more than one data type stored in them.

This is a Union:

        union var {
                int x;
                float f;
        };

What is special about a union is that it holds one block of memory for multiple values. Each time a variable is assigned a value in the code, the old value in the memory is replaced with the new.

Struct:

        struct person{
                char *name;
                unsigned char age;
                short int weight;
                float gpa;
        };

A struct can have more than one block of memory, one for each of the values in the struct.

Analysis

Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.

  • How did you do?

– From what I was able to type up, it seems that my understanding of it now is pretty good.

  • Is there room for improvement?

– There is room for improvement, I still have a little trouble with creating these and implementing them, but with a little practice, I should be fine.

  • Could the measurement process be enhanced to be more effective?

– It could be, I could have implemented code and shown the result.

  • Do you think this enhancement would be efficient to employ?

– It would be efficient to employ, it would help me understand these concepts more.

  • Could the course objective be altered to be more applicable? How would you alter it?

– It probably could be, in the way that I mentioned.

opus/spring2012/jcavalu3/cprogpart3.txt · Last modified: 2012/05/09 22:13 by jcavalu3