• logic and operators (and, or, not, xor) • Scope (Block, Local, Global, File) • Type Casting • Structures (Declaration, Accessing Elements, Pointers to) • Type Casting Operators, Const-Volatility Specifiers (const, volatile) [C++] • Overloading (Functions, Operators) [C++] • Exception Handing (throw, try, catch) [C++] • Templates, STL (Standard Template Library) [C++]
logic operators are a logical operation on one or more logic inputs that produces a single logic output
a b | and or xor nor xnor nand notb nota F T | F T T F F T F T T T | T T F F T F F F F F | T F F T T T T T T F | F T T F F T T F
Converting one type of data into another type of data.
short a=2000; int b; b = (int) a; b = int (a); or short a=2000; int b; b=a;
Templates in C++ template are classes to provide common programming data structures and functions. STL- standard template library is a library of templates
#include<iostream> using namespace std; template <class T> void myfunc (T val1, T val2, T val3) { T result; result = val1 + val2 + val3; cout << "First value is: " << val1 << endl; cout << "Second value is: " << val2 << endl; cout << "Third value is: " << val3 << endl; cout << "The sum of all three are: " << result << endl; cout << "The average of all three are: " << (T)(result/3) << endl; } int main() { // some c code here return 0; }
You can redefine a function of most built-in operators in C++ by overloading globally or on a class. key point * Overloaded operators are implemented as functions and can be “member functions or global functions”.
You can also overload a function name by declaring more than one function with the that name in the same scope. when the program is being run and the function is call the program matches parameters up to select the right function.
Operator overloading
#include <iostream> using namespace std; class complx { double real, imag; public: complx( double real = 0., double imag = 0.); // constructor complx operator+(const complx&) const; // operator+() }; // define constructor complx::complx( double r, double i ) { real = r; imag = i; } // define overloaded + (plus) operator complx complx::operator+ (const complx& c) const { complx result; result.real = (this->real + c.real); result.imag = (this->imag + c.imag); return result; } int main() { complx x(4,4); complx y(6,6); complx z = x + y; // calls complx::operator+()
and now function overloading
#include <iostream> using namespace std; void print(int i) { cout << " Here is int " << i << endl; } void print(double f) { cout << " Here is float " << f << endl; } void print(char* c) { cout << " Here is char* " << c << endl; } int main() { print(10); print(10.10); print("ten");
and now the output from this function over loading
lab46:~$ ./funoverl Here is int 10 Here is float 10.1 Here is char* ten
key words that can “catch exceptions” by placing a portion of code under exception inspection and enclosing that portion of code in a “try block”. it works When an exception circumstance comes about within that block, the exception is then thrown which transfers the control to the exception handler. If no exception is thrown the code continues normally.
#include <iostream> using namespace std; int main () { try { throw 20; } catch (int e) { cout << "An exception occurred. Exception Nr. " << e << endl; } return 0; }
block: C++ names can only be used in parts of a program called the “scope” of the name. unless otherwise acres from a member of that class scope. this is very important in C++ because scope determines when variables local to the scope are initialized.
Local scope:A name declared within a block is accessible only within that block and blocks enclosed by it and only after the point of declaration.
File scope: Any name declared outside all blocks or classes has file scope also known as namespace scope.
global scope: is names with file scope that are not declare objects are global names.
local scope { int i; }
Notice the declaration of i is in a block enclosed by curly braces so i has local scope and is never accessible because there is no code accesses before the closing curly brace.
class area() { int x; int y; };
x and y can be used to access the class area through “.”or “→”
constant : makes that the type is constant but also the object shall not be modified. In doing so it may you may recieve undesired/undefined results such as compile time error compile-time error.
volatile: makes the type volatile and the object then be modified so that the compiler doesn't yell at you.
const int five = 5; const double pi = 3.141593;
const objects may not be changed below is an example: <Code>
#include <stdio.h> #include <stdlib.h> int main() {
int result=0; const int five = 5; const double pi = 3.141593;
pi = 3.2; five = 6; result=five+pi;
return(0); } </code> compile msg below
lab46:~$ gcc -o const const.c const.c: In function 'main': const.c:9: error: assignment of read-only variable 'pi' const.c:10: error: assignment of read-only variable 'five'
Declaring : a structure it is in the form of a block of data which can contain different data types depending on means of a structure declaration.
Accessing elements: structure accessing is done by specify both the structure name (name of the variable) and the member name when accessing information stored in a structure.
Point:you can point to structs by deference them below is example
struct tag *st_per;
and we point it to our example structure with:
st_per = &my_struct;
struct person{ char *name;//or --char name[20]; unsigned char age; short int weight; float gpa; }; //struct differ from union bec struct allocate memory for each and dont share $ typedef struct person P; P p1,p2,p3,p4,p5; struct person p1; struct person p2; struct person p3,p4,p5; Accessing Members of a Structure p1.age = 29; point to : struct person *st_per; st_per = &my_struct; (*st_per).age = 63;
The course objective is to be able to successfully develop C code of a desired program. The program should work and any problems that come about can be figure out and finally manage the code with C++ and abstract details away for security and neatness.
Develop a program that will allow a simpler way of doing something from creating a program of your choice. Have what you wish to be able to do and then program with the requirements chosen. produce a finished result
I have develop a program that i thought was going to be easy and turned out my requirement i want i did not achieve so i changed them a little. the program accepts string and compares string to string for user authentication.
Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.