Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

C++ Programming Code Examples

C++ > Code Snippets Code Examples

A Class as a Member Variable of Another Class

/* A Class as a Member Variable of Another Class This is an example of one object (the shape class implements a rectangle) being a member variable of another object (a brick). */ Header File: shape.h #ifndef _SHAPE_H #define _SHAPE_H class FRectangle { public: FRectangle(double l = 0, double w = 0) : Length(l), Width(w) {} void setLength(double lgt); void setWidth(double wdt); double getLength() const; double getWidth() const; double Perimeter() const; double Area() const; void Properties(); private: double Length; double Width; }; #endif // _SHAPE_H Source File: shape.cpp #include <iostream.h> #include "shape.h" void FRectangle::setLength(double lgt) { Length = lgt; } void FRectangle::setWidth(double wdt) { Width = wdt; } double FRectangle::getLength() const { return Length; } double FRectangle::getWidth() const { return Width; } double FRectangle::Perimeter() const { return 2 * (Length + Width); } double FRectangle::Area() const { return Length * Width; } void FRectangle::Properties() { cout << "\nRectangle characteristics"; cout << "\n\tLength = " << Length; cout << "\n\tWidth = " << Width; cout << "\n\tPerimeter = " << Perimeter(); cout << "\n\tArea = " << Area() << endl; } Header File: brick.h #ifndef BRICK_H_ #define BRICK_H_ #include "shape.h" class Brick { public: Brick() {} void setThickness(double Tck); void setDimensions(double l, double w, double t); void setColor(char* clr); void setTexture(char* txr); char* getColor() const; char* getTexture() const; double Volume() const; void Display(); private: FRectangle shape; char* Color; char* Texture; double Thickness; }; #endif // BRICK_H_ Source File: brick.cpp #include <iostream.h> #include "brick.h" void Brick::setThickness(double Tck) { Thickness = Tck; } void Brick::setColor(char* clr) { Color = clr; } void Brick::setTexture(char* txr) { Texture = txr; } void Brick::setDimensions(double l, double w, double t) { shape.setLength(l); shape.setWidth(w); setThickness(t); } char* Brick::getColor() const { return Color; } char* Brick::getTexture() const { return Texture; } double Brick::Volume() const { return shape.getLength() * shape.getWidth() * Thickness; } void Brick::Display() { cout << "\nBrick characteristics"; cout << "\n\tLength = " << shape.getLength(); cout << "\n\tWidth = " << shape.getWidth(); cout << "\n\tArea = " << shape.Area(); cout << "\n\tVolume = " << Volume(); cout << "\n\tColor = " << getColor(); cout << "\n\tTextture = " << getTexture(); cout << endl; } Main File: Exo.cpp #include "shape.h" #include "brick.h" void main() { Brick brick; brick.setDimensions(12.50, 8.75, 5.55); brick.setColor("Bone White"); brick.setTexture("Early Breeze"); brick.Display(); } Here is an example of running the program: Brick characteristics Length = 12.5 Width = 8.75 Area = 109.375 Volume = 607.031 Color = Bone White Textture = Early Breeze

The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

In the C++ Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions. The syntax for creating a constant using #define in the C++ is: #define token value

setcolor() function is used to set the foreground color in graphics mode. After resetting the foreground color you will get the text or any other shape which you want to draw in that color. setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. The current drawing color is the value to which pixels are set when lines, and so on are drawn. The drawing colors shown below are available for the CGA and EGA, respectively.

The #ifndef directive of the C++ Programming Language helps in allowing the conditional compilation. The C++ Programming Language's preprocessor helps in determining only if the macro provided is not at all existed before including the specific subsequent code in the C++ compilation process. The #ifndef preprocessor only checks If the specific macro is not at all defined with the help of the #define directive. If the condition is TRUE then it will be helpful in executing the code otherwise the else code of the #ifndef will be compiled or executed only if present.

Return length of string. Returns the length of the string, in terms of bytes. This function is used to find the length of the string in terms of bytes. This is the actual number of bytes that conform the contents of the string , which is not necessarily equal to the storage capacity. This is the number of actual bytes that conform the contents of the string, which is not necessarily equal to its storage capacity. Note that string objects handle bytes without knowledge of the encoding that may eventually be used to encode the characters it contains. Therefore, the value returned may not correspond to the actual number of encoded characters in sequences of multi-byte or variable-length characters (such as UTF-8).

In C++, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C++ has the same name as class or structure. Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object. • Default Constructor: A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.

rectangle() is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner.

Get/set field width. The first form (1) returns the current value of the field width. The second form (2) also sets a new field width for the stream. The field width determines the minimum number of characters to be written in some output representations. If the standard width of the representation is shorter than the field width, the representation is padded with fill characters at a point determined by the format flag adjustfield (one of left, right or internal). The fill character can be retrieved or changed by calling the member function fill. The format flag adjustfield can be modified by calling the member functions flags or setf, by inserting one of the following manipulators: left, right and internal, or by inserting the parameterized manipulator setiosflags.

We create a queue for BFS. Mark the current node as visited and enqueue it. It will be used to get all adjacent vertices of a vertex. Get all adjacent vertices of the dequeued vertex s. If