I/O Streams (cin, cout, cerr, stream operators)
The concepts of I/O in C++ are streams, insertion, and extraction. The input stream is a source of input. It can be an object from which characters can be obtained (extracted). The output stream is an object to where output can be directed.
Here is and example
#include <iostream> #include <fstream> int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; }
Namespaces [C++]
A namespace is a declaration of different areas of the code. It calls them by a name and is then called out from within the code within the cycle of workflow.
Here is an example of the arrangement that a namespace takes within the code.
#include <iostream> using namespace std; namespace first { int var = 5; } namespace second { double var = 3.1416; } int main () { cout << first::var << endl; cout << second::var << endl; return 0; }
Classes
Classes are groups of data related to a single object type. They are really functions to access information, and the classes possess the ability to inherit from other classes.
Here is an example from wiki. This shows “class” being used to define the called variable “person”. Then person is further allocated to have two containers, a and b.
#include <iostream> #include <string> using namespace std; class person { public: string name; int age; }; int main() { person a, b; a.name = "Calvin"; b.name = "Hobbes"; a.age = 30; b.age = 20; cout << a.name << ": " << a.age << endl; cout << b.name << ": " << b.age << endl; return 0; }
Inheritance
Inheritance is a a structure of reusing existing classes that have already been declared without modifying them, thus producing hierarchical relationships between them.
In the following example the class “A” is called out as public making it inherit the past declaration.
#include <iostream> using namespace std; class A { int data; public: void f(int arg) { data = arg; } int g() { return data; } }; class B : public A { }; int main() { B obj; obj.f(20); cout << obj.g() << endl; }
Overloading
Overloading allows functions to have the same name but to have different parameters. So operators can be extend to act as a class within the same code.
Here is an example from wiki. The “time” operators is declared at the code header and given meaning. It is then broken into minutes and seconds.
Time operator+(const Time& lhs, const Time& rhs) { Time temp = lhs; temp.seconds += rhs.seconds; if (temp.seconds >= 60) { temp.seconds -= 60; temp.minutes++; } temp.minutes += rhs.minutes; if (temp.minutes >= 60) { temp.minutes -= 60; temp.hours++; } temp.hours += rhs.hours; return temp; }
Exception Handling
Exception Handling is the attempt to have foresight into problems that happen while the code is running. Planning ahead for the inability of the program to do what it is designed to do, like being unable to find the source file that is used to pull up a list of data. While righting you code it is a good idea to “throw and exception” and see how the code responds.
Here is an example where we are asking the user for two numbers… what if they add a letter?
cout << "Please provide two numbers\n"; cout << "First Number: "; cin >> a; cout << "Second Number: "; cin >> b;
Adding in an If statement when the input is used can handle this exception and have fun telling the user to go back to Kindergarten.
If (a='A') cout << "sorry bro...that is a letter.. and number is the ones you use when you count your pennys";
Templates, STL
STL is short for Standard Template Library. The templates are a standard list of classes and template code.
STL provides many different types of container types. One is called a vector. It is sets a block allocated for use and can grow as needed (something that would make bignum easier). Here is an example i found. You can see the used of the vector.h file and the allocating of it. “vector<int> v;”.
#include <string.h> #include <algo.h> #include <vector.h> #include <stdlib.h> #include <iostream.h> main () { vector<int> v; // create an empty vector of integers int input; while (cin >> input) // while not end of file v.push_back (input); // append to vector sort(v.begin(), v.end()); int n = v.size(); for (int i = 0; i < n; i++) cout << v[i] << "\n"; }
Use a C program to change the IP address of my computer.
While doing other objectives I came across the suggestion that you should change your IP at given increments to increases your privacy while surfing. The documentation i was reading had more to do with other “outlets” for creative computing; however my love of computers comes from finding the limits of what can or cannot be done.
Explore the world for a walk-through or community documentation. Compile that information and implement.
To change your ip the standard ifconfig way
# ifconfig eth0 192.168.1.5 netmask 255.255.255.0 up # ifconfig eth0
Here is the direct code from lainoox that works very nicely. I would suggest that a script on playing with ifconfig would just be simpler. This is one approach.
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> #include <net/if.h> #include <net/if_arp.h> #include <sys/ioctl.h> #include <linux/sockios.h> #include <errno.h> #include <netinet/in.h> #if defined(__GLIBC__) && __GLIBC__ >=2 && __GLIBC_MINOR__ >= 1 #include <netpacket/packet.h> #include <net/ethernet.h> #else #include <sys/types.h> #include <netinet/if_ether.h> #endif int set_ip(char *iface_name, char *ip_addr) { if(!iface_name) return -1; int sockfd; struct ifreq ifr; struct sockaddr_in sin; sockfd = socket(AF_INET, SOCK_DGRAM, 0); if(sockfd == -1){ fprintf(stderr, "Could not get socket.\n"); return -1; } /* get interface name */ strncpy(ifr.ifr_name, iface_name, IFNAMSIZ); /* Read interface flags */ if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) { fprintf(stderr, "ifdown: shutdown "); perror(ifr.ifr_name); return -1; } /* * Expected in <net/if.h> according to * "UNIX Network Programming". */ #ifdef ifr_flags # define IRFFLAGS ifr_flags #else /* Present on kFreeBSD */ # define IRFFLAGS ifr_flagshigh #endif // If interface is down, bring it up if (ifr.IRFFLAGS | ~(IFF_UP)) { fprintf(stdout, "Device is currently down..setting up.-- %u\n",ifr.IRFFLAGS); ifr.IRFFLAGS |= IFF_UP; if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) { fprintf(stderr, "ifup: failed "); perror(ifr.ifr_name); return -1; } } sin.sin_family = AF_INET; // Convert IP from numbers and dots to binary notation inet_aton(ip_addr,&sin.sin_addr.s_addr); memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr)); // Set interface address if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) { fprintf(stderr, "Cannot set IP address. "); perror(ifr.ifr_name); return -1; } #undef IRFFLAGS return 0; } void usage() { const char *usage = { "./set_ip [interface] [ip address]\n" }; fprintf(stderr,"%s",usage); } int main(int argc, char **argv) { if(argc < 3){ usage(); return -1; } set_ip(argv[1],argv[2]); return 0; }
Out put of the program
ifconfig eth1 eth1 Link encap:Ethernet HWaddr 00:1b:21:0a:d2:cf inet addr:192.168.5.12 Bcast:192.168.5.255 Mask:255.255.255.0 inet6 addr: fe80::21b:21ff:fe0a:d2cf/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2690 errors:0 dropped:0 overruns:0 frame:0 TX packets:14732 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 RX bytes:516826 (516.8 KB) TX bytes:2242645 (2.2 MB) $ sudo ./set_ip eth1 12.13.14.15 Device is currently down..setting up.-- 4163 $ ifconfig eth1 eth1 Link encap:Ethernet HWaddr 00:1b:21:0a:d2:cf inet addr:12.13.14.15 Bcast:12.255.255.255 Mask:255.0.0.0 inet6 addr: fe80::21b:21ff:fe0a:d2cf/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2690 errors:0 dropped:0 overruns:0 frame:0 TX packets:14742 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 RX bytes:516826 (516.8 KB) TX bytes:2247309 (2.2 MB
Here is a shell script route
#!/bin/bash SUBNET=192.168.135. ETH=eth0 for i in {1..254} do ip addr add ${SUBNET}${i}/24 dev ${ETH} # do whatever you want here ip addr del ${SUBNET}${i}/24 dev ${ETH} done
Sources
Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.