Search

воскресенье, 8 апреля 2012 г.

Study day part 3: Links of Nodes

Linklists. Very important part of programing. Not as confusing as multiple Inheritance, but still pretty confusing. What's a linklist? It's a Main Manager Object and many smaller ones (Nodes) that run around. I won't use any full examples cuz' it will take A LOT of lines to do so, but I will provide functions that can be used. So, I guess we should start with declaration:
class List;

class Node{
   int _data;
   Node* _next;  Node* _prev;
   Node(int data, Node* next = (Node*)0);
   friend class List;
};

class List{
   Node* _head;
   Node* _tail;
   Node* _cur;
public:
   List();
   ...(functions)
   virtual ~List();
};
Pretty strait forward. Next. Constructors & Destructor:
Node::Node(int data, Node* next, Node* prev){
   _data = data;
   _next = next;
   _prev = prev; 
   etc...
Queue::Queue(){
   _head = (Node*)0;
}
Queue::~Queue(){
   while(!isEmpty()) removeHead();
}
Now. After we've covered basics, we should move to functions themselves, and we should start with the most basic function possible "isEmpty" because it's used in most methods.
bool Queue::isEmpty(){
   return !_head;
Nature of a linklist is that if there is nothing at the head, there is nothing in the entire list. So we use it here.
After that, we should add something to the head of the list:
void Queue::add(int data){
   if(isEmpty()){
     _head = new Node(data);
   }else{
      Node* cur = _head;
      Node* tail = cur;
      while(cur){
      tail = cur;
      cur= cur->_next;
   }
   tail->_next = new Node(data);
   }
}

int Queue::remove(){
   int ret = _head->_data;
   Node* ToDel = _head;
   _head = _head->_next;
   delete ToDel;
   return ret;
}

This should cover at least most of it.
 

 

Study day part 2: Bits and Bites

Bitwise operator (I hope I'm spelling it correctly). They aren't that complicated, and I spend most of the break between semester helping a friend with that., so I got used to it. The only problem I have with them is that... Unless you create a function that displays a value by bits working with them is... theoretical. Best thing you can see is "a number changed" and that's it. But they work, and as far as I know very effectively.
First, Let's look at operators themselves.
Operators in binary are exactly like if statements.
AND
1. & - AND. Basically the chart is this:
//I should be a bit more exotic with my tags, only oop344 and c++ isn't enough x)
0 & 0 = 0  //if(false && false) == false
1 & 0 = 0  //if(true && false) == false 
0 & 1 = 0  //if(false && true) == false
1 & 1 = 1  //if(true && true) ==  true
Example:
   A = "0011" //== 3
&
   B = "0101" //== 5
=
         "0001" //== 1
So:
   A & B = 1

inclusive OR 

2. | -  inclusive OR. Basically the chart is this:
//I should be a bit more exotic with my tags, only oop344 and c++ isn't enough x)
0 | 0 = 0  //if(false || false) == false
1 | 0 = 1  //if(true || false) == true  
0 | 1 = 1  //if(false || true) == true  
1 | 1 = 1  //if(true || true) ==  true 

Example:
   A = "0011" //== 3
|
   B = "0101" //== 5
=
         "0111" //== 7
So:
   A | B = 7



exclusive OR 
3. ^ -  exclusive OR, it's a thing that you won't find in if statements, after previous ones it's not that hard. Basically the chart is this:
//I should be a bit more exotic with my tags, only oop344 and c++ isn't enough x)
0 ^ 0 = 0  //if(false | false) == false
1 ^ 0 = 1  //if(true | false) == true  
0 ^ 1 = 1  //if(false | true) == true  
1 ^ 1 = 0  //if(true | true) ==   false  

Example:
   A = "0011" //== 3
^
   B = "0101" //== 5
=
         "0110" //== 6
So:
   A | B = 6

NOT
4.  ~ - NOT operator. Basically what is does if flips all the bits of a value.Example
3 = "0011"
~3 = "11111111111111111111111111111100"

Now the "fun part". Bit movement!

Right Shift
5. >>X - Right Shift operator.
Moves all the bits to the right X times.
A = "0101"; //3
A>>1="0010"; //2
A>>2="0001"; //1
A>>2 = 1;
Also, Right Shift once == divide by two.

Left Shift
 6. <<X - Left Shift operator.
Moves all the bits to the left X times.
WARNING!Apparently, there is a difference in what kind of value are you shifting. Unsigned or signed. If int is unsigned  it will use 0's to fill in the space, but if it's signed, it will use the last bit (if 1 - 1's, if 0 - 0's) to do that.
unsigned A = "0011"; //3
A<<1="0110"; //6
A<<2="1100"; //12
A<<2 = 1; 12
Also, Left Shift once == Multiply by two.

signed A = "0011"; //3
A<<1="0111"; //7
A<<2="1111"; //15
A<<2 = 15;


Example:
#include <cstdio>
using namespace std;

int main(){
unsigned char A = 0xA3;
unsigned char B = 0xF9;
unsigned char C;
printf("A: %X %d\n", A,A); //163 = "1010 0011"
printf("B: %X %d\n", B,B); //249 = "1111 1001"
printf("======================================\n");
C = A & B;
printf("A&B: %X %d\n", C, C); //161 = "1010 0001"
C = A | B;
printf("A|B: %X %d\n", C, C); //251 = "1111 1011"
C = A ^ B;
printf("A^B: %X %d\n", C, C); //90 = "0101 1010"
C = ~A;
printf("~A : %X %d\n", C, C); //92 = "0101 1100"
C = A << 1;
printf("A<1: %X %d\n", C, C); //70 = "0100 0110"
C = A >> 1;
printf("A>1: %X %d\n", C, C); //81 = "0101 0001"
printf ("(A >> 4) << 4:\n");
C = A >> 4;
printf("A>4: %X %d\n", C, C);//10 = "0000 1010"
C = C << 4;
printf("A<4: %X %d\n", C, C); //160 = "1010 0000"
printf ("(A >> 4) << 4 = %X %d\n", C, C);
return 0;
}

Study day part 1: Copycat

So, what's a copy constructor? I donno. I mean I do, but I forgot. To Wikipediaaaa!!
A copy constructor is a special constructor in the C++ programming language for creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed (const or non-const), which might be followed by parameters of any type (all having default values).
 Examples of these are:
cclass(const cclass& copy) 
{
     this->num = copy.num;
}
The following cases may result in a call to a copy constructor:
1. When an object is returned by value
cclass CC = getClass(num);
2. When an object is passed (to a function) by value as an argument
foo(CC);
3. When an object is thrown
throw CC;
4. When an object is caught
catch (CC)

And, to finish off, I need a full example.
    ////*class*////
Point::Point(const Point& p) {
   x = p.age;
   y = p.num;
}
   ////*Main*////
Point p;       // calls default constructor
Point s = p; // calls copy constructor.
p = s;          // assignment, not copy constructor, need to overload the "=" operator

Study day part 0: Da List!

Ok, so, test is tomorrow. And I know I should sit all night and study, instead of getting a good sleep. So! I'm going to study all day, and prepare my notes.
Cocke, dubstep and notes. Let's do this. I will start with the list of things I need to repeat and/or include in my notes. All next post will be related to each one of these (I hope). Here is the list:
1. Copy constructors. Why do we need them and where are we using them?
2. Template linklists. I NEED a good example of that.
3. Binary. Because they will be on the test.
4. Binary Files. Same here.
5. Multiple inheritance. OF COURSE! (c)
6. Exceptions. Won't hurt.
7. Command line arguments. I know them, but just in case.
Also, this list will be updated with link to related posts, just in case.

понедельник, 20 февраля 2012 г.

Console Resolved

YAAAYYY!! Console is fixed! What went wrong? I donno. Apparently, putty doesn't like cout when I use it in my display function, or something.
Before it was:

     std::cout<<tstr;
 I made it like this:
   console<<tstr;
Which is basically a "putchar" statement. I guess it has something to do with the internal putchar and cout differences, but might be wrong, as far as I know it works, and that's good!
I submitted 0.1, have about a week to make 0.2 are we're gonna split the functions between out group in a few days, so I might as well finish with the wiki and have a few hours of rest, on this last day of the long weekend!

Console

So, it's bern a while, since I bloged about something... (Note to self: Don't forget to upload thing on wiki)
Ok, new year, old (sort of) assignment. Everything should go fine... Not. The fist problem I encountered was right at the beginning.
The assignment starts with writing a "console.cpp", basically, a display and edit function for a string of text, noting major... And I did. And it worked... On Visual Studio. I'm not sure why, but in unix environment my function isn't showing the string and just doing all sots of illogical things with the string. Apparently it has something to do with memory handling in the unix.
So, teacher didn't answered my email about an appointment (I should talk with him about the   procedure of "successfully sending an email") I have my goldfish crackers, some water and a few hours of work!

I guess I should start with taking a regular console tester and checking if it works, next, I'm going to work on display function, until I at least will be able to see the sting... Yeah, that should do it.

воскресенье, 15 января 2012 г.

Happpy new OOP year!

   So... I failed C++ last semester. Why? Well, first of all I was a bit too lazy, and unlucky - all the staff I was preparing for some reason got completely useless on the exam and second test. So yeah, I barely made it through test one, failed test two, I guess I should've expected something like this on the exam.
This semester I'm planning to fix stuff I failed last time, let's see...
- Learn Templates
- Repeat Link lists
- Learn how they can work together
- Get a book (I'll probably work on it this week)
- Repeat files and binary
This should cower... most of the course, I will also try to make appointments with the teacher so I can insure that I know everything... Most of it.
   Ok, this looks right. I hope things will work out for me this time, I'm not sure about the team, but it will be alright. I guess I should go to sleep... Classes at 8 am is a fair "punishment" for my laziness.
Good luck for me and everyone who's reading!