The daily ramblings …

C++ : I/O Streams – Output Formatting

Posted in C++ General by akamal on February 12, 2007

Creating a clean formatted data output is a common programming requirement. Take note however that output formatting requires the use of iomanip.h file, so don’t forget to include it. Anyway, listed below is a collection of tricks to use in C++.

(more…)

Advertisements

C++ : Static

Posted in C++ General by akamal on January 26, 2007

Static data types can be accessed without the instantiation of the class in C++. Applicable for static functions too. A typical use of static members is for recording data common to all objects. E.g: We can use static data to record the number of objects created.

To access static member, we uses ::(scope resolution) operator. An example is shown below:

01#include <iostream>
02using namespace std;
03
04class Circle
05{
06private:
07	// Initially declare radius as type double and static
08	static double radius;
09public:
10	Circle() {} // constructor
11	static void setRadius(double var)
12	{
13		radius = var;
14	}
15	static double getRadius()
16	{
17		return radius;
18	}
19	static double getArea()
20	{
21		return radius * radius;
22	}
23};
24
25double Circle::radius = 30;
26
27int main()
28{
29	Circle circ;
30	cout << "Circle 1 radius is : " << Circle::getRadius() << "n";
31	// object circ can also access static method getArea
32	cout << "Circle 1 area is : " << circ.getArea() << "n";
33	Circle::setRadius(50);
34	cout << "Circle 1 radius is now : " << Circle::getRadius() << "n";
35	cout << "Circle 1 area is : " << Circle::getArea() << "n";
36	system("PAUSE");
37	return 0;
38}

From the example above, we don’t need to instantiate the class to access static method “method1”. So, that’s the plain use of static.

Remember the rules of thumb below:

1 – Static members can be called even when no instantiation of class is made. Normal members of class (non-static) can only be called when an object is instantiated out of the class.

2 – Static member / funcntion cannot have access to ‘pointer of the’ class. Meaning that pointer to a class will have its own members, and the static member won’t have access to that.

3 – Static member function cannot be declared virtual, while non-static member can.

4 – Static data members must be defined out side of class definition, because they don’t belong to any specific instance of the class.

01class X
02{
03public:
04	static int i;
05};
06// definition outside class declaration
07int X::i = 0;

5 – Static member functions get defined in the class and so should static member variables.

C++ : Methods of Object Instantiations with Classes

Posted in C++ General by akamal on January 16, 2007

When it comes to programming, the best method to learn is by digging up codes from existing source codes, but question remains, how do you learn by looking at thousands of lines of codes? Well firstly, you have to get your basics right.

To recap, Classes and OOP are the main feature provided by C++. With classes, you’d be able to expand your built codes for other future needs, or simply to tackle similar problems by re usability. The purpose of this article is not to look at classes (well not now), but rather a quick peek on the styles of code-writing when instantiating / using classes. Remember that the concept of class is strongly linked to pointers, especially in C++. So you might want to get a brush-up on it first. Look it up here.

Fundamentally, there are 2 styles when writing class instantiations.

  1. The first style assigns a variable with the instantiation of the object itself (as seen in type A).
  2. The second, goes for a more versatile approach by using a variable to store the address of the created object (type B).

type B:

1// We assign a variable named window in line 9. 
2// Its type is not int, char or whatever, but instead it is an 
3// object of class QWidget.
4// To access its properties, we use "." to access its method,
5// as seen in line 10 --- window.show()
6int main(int argc, char * argv[])
7{
8	QApplication app(argc,argv);
9	QWidget window;
10	window.show();
11	return app.exec();
12}

type B:

1// here, we initially declare a pointer named window 
2// which is of type object. Variable window be allocated in the 
3// memory with no value. Next, we assign the window variable to the 
4// address of the QWidget object just created (new QWidget creates an object).
5// To access its properties, we use "->", known as throwing at, 
6// to access method show() as seen in line 6.
7int main (int argc, char * argv[])
8{
9QApplication app(argc,argv);
10QWidget *window;
11window = new QWidget;
12window->show();
13return app.exec();
14}

Between the 2, there’s aren’t many differences generally, and certainly no pros/cons that I know of. But just to get some people accustomed to the style of writing, one should be aware that class instantiation can be done in 2 ways, as explained above.

C++ : The Concept of Pointer

Posted in C++ General by akamal on January 16, 2007

I was doing my normal programming routine today, when halted for request on ad hoc pointer tutorial by some of my team members. The 1 hour & 30 minutes tutorial spoke for itself, seeing how hard it is for newcomers to grasp the concept.

Throughout the session, questions bombarded ranging from “why the need to pass between functions” to “how important as to store address of a var”? and so on.

So I’ve come up with this small article on this, just a reminder to those who’s so regularly confused with the concept, I know I do.

[JUSTIFICATION]
When programming for software requiring large design, there’ll be hundreds, literally thousands of codes meddling in functions here and there. Especially on a critical / memory heavy s/w, unnecessary usage of memory is certainly a thing to avoid.

The use of pointer allows programmer to keep on using the same variable, WITH the same memory location, passing it to any external functions so as long it requires.

A safe practise of programing always involves pointers, especially in C++. Since we do not have the luxury of automatic memory management, as Java does, those who codes in C++ must always keep in mind on variable allocation and de-allocation (aka deletion).

[IMPORTANT SYMBOLS]

& is a reference operator. Also means “address of variable to point to”
* means “is a dereference operator. Also means value pointed by the variable”

[EXPLANATION]

To understand what the 2 above means, please have a look at the codes below. No point dwelling on lengthy explanations, right? I’d better kick it off with codes review! Let’s begin..

Ok, in line 1 you have a variable var1 of type, say, int. var1 is then assigned the value of 25. Next, you declare

[EXAMPLE SNIPPET]
int var1 = 25;
int *var2;
var2 = &var1;
cout << var2;
cout << *var2;

1 int var1 = 25;
2

var1 = 25; // var1 hold the value 25 in some memory address, say address 1000
2 var2 = &var1; // var2 stores the address of var1, so when typing …
3 cout << var2; // you would get the address of var1, which is 1000
4 var3 = *var2; // now var3 will take the value pointed by var2
5 // var2 has address 1000, and value pointed by address 1000 is 25

[COMMENT]
The funny thing is a lot of people may understand this concept through books and tutorials, but
when it comes to “real” programming, they’d be perplexed with the way values and address are passed all
over.

I say, that the only way to fully grasps this concept, is to practice regularly with pointers, yeah.