Skip to main content

Posts

Showing posts with the label Class

A simple robot program

Creating a robot program has been designed to be as simple as possible while still allowing a lot of flexibility. Here’s an example of a template that represents the simplest robot program you can create. #include "WPILib.h" class RobotDemo : public SimpleRobot { RobotDemo(void) { // put initialization code here } void Autonomous(void) { // put autonomous code here } void OperatorControl(void) { // put operator control code here } }; START_ROBOT_CLASS(RobotDemo); There are several templates that can be used as starting points for writing robot programs. This one, SimpleRobot is probably the easiest to use. Simply add code for initializing sensors and anything else you need in the constructor, code for your autonomous program in the Autonomous function, and the code for your operator control part of the program in OperatorControl. SimpleRobot is actually the name of a C++ class or object that is used as the base of this robot program called RobotDemo...

Program that performs selection search in C++ Programming

#include <iostream.h> #include <conio.h> class sel_search { int d[50],s,search_val; public: void getdata(void); int search(void); void display(void); }; void sel_search :: getdata(void) { cout<<endl<<endl; cout<<"How many size of array you want to create:-"; cin>>s; cout<<"Enter "<<s<<" Integers\n"; for(int i=0;i<s;i++)     cin>>d[i]; cout<<"\n\nEnter your search:-"; cin>>search_val; } int  sel_search :: search(void) { for(int i=0;i<s;i++) {     if(d[i]==search_val)            return(i+1); } return(-1); } void sel_search :: display(void) { int result; cout<<"\n\n\n"; result=search(); if(result==-1)     cout<<"\nEntered Search is Illegal\n"; else     cout<<"\nSearch is Located at "<<result<<" Position"; } void main() { clrscr(); sel_search o1; o1.getdat...

Program of Scaner

#include<stdio.h> #include<conio.h> #include<ctype.h> #include<string.h> #include<iostream.h> #include<stdlib.h> int isoperator(char ch)   {     if(ch=='+')         return 1;     elseif(ch=='-')         return 2;     elseif(ch=='*')         return 3;     elseif(ch=='/')         return 4;     elseif(ch=='=')         return 5;     elsereturn 0;   } /*int issymbol(int symcounter,char symbol[][10],char currstr[])   {     for(int i=0;i<symcounter;i++)       {         if(strcmp(symbol[i],currstr)==0)             return i;       }     return -1;   }*/ void main() {     char instr[50];     char currstr[50];     char...

Program of Scaner

#include<stdio.h> #include<conio.h> #include<ctype.h> #include<string.h> #include<iostream.h> #include<stdlib.h> int isoperator(char ch)   {     if(ch=='+')         return 1;     elseif(ch=='-')         return 2;     elseif(ch=='*')         return 3;     elseif(ch=='/')         return 4;     elseif(ch=='=')         return 5;     elsereturn 0;   } /*int issymbol(int symcounter,char symbol[][10],char currstr[])   {     for(int i=0;i<symcounter;i++)       {         if(strcmp(symbol[i],currstr)==0)             return i;       }     return -1;   }*/ void main() {     char instr[50];     char currstr[50];     char...

Program of cector class that perform different operations on vector in C++ Programming

#include <iostream.h> #include <conio.h> intconst size=50; class vector { float d[size]; int s; public: void create(void); void modify(void); void multiply(void); void display(void); }; void vector :: create(void) { cout<<"\n\nEnter of Array you want to create:-"; cin>>s; cout<<"Enter "<<s<<" Real Numbers\n"; for(int i=0;i<s;i++)        cin>>d[i]; } void vector :: modify(void) { int mfy_value; float with; cout<<"\nEnter Value is to be modified:-"; cin>>mfy_value; cout<<"Enter Value with which you want to Replace:-"; cin>>with; int search=0; for(int i=0;search<s;i++) {     if(mfy_value==d[i])        {       d[i]=with;       }    search=i+1; } } void vector :: multiply(void) { int mul; cout<<"\nEnter value with which you want to multiply:-"; cin>>mul; for(int i=0...

Shallow And Deep copying

A shallow copy means that C++ copies each member of the class individually using the assignment operator. When classes are simple (eg. do not contain any dynamically allocated memory), this works very well. For example, let’s take a look at our Cents class: class Cents { private:     int m_nCents; public:     Cents(int nCents=0)     {         m_nCents = nCents;     } }; When C++ does a shallow copy of this class, it will copy m_nCents using the standard integer assignment operator. Since this is exactly what we’d be doing anyway if we wrote our own copy constructor or overloaded assignment operator, there’s really no reason to write our own version of these functions! However, when designing classes that handle dynamically allocated memory, memberwise (shallow) copying can get us in a lot of trouble! This is because the standard pointer assignment operator just copies the address of the pointer — it does not ...

Program To Input The Flight Details and Print It

#include<iostream.h> #include<conio.h> class flight { public: int flight; char destination; int distance; public : void flight::feeddata(void) { cout<<"enter the flight no.:"; cin>>flight; cout<<"enter the destination:"; cin>>destination; cout<<"enter the distance:"; cin>>distance; } void flight::showdata(void) { cout<<"the flight no. is:"<<flight; cout<<"the flight will go to:"<<destination; cout<<"the distance is:"<<distance; } }; void main() { clrscr(); flight plane; plane.feeddata(); plane.showdata(); getch(); }

What is Recursion

Simply put, recursion is when a function calls itself. That is, in the course of the function definition there is a call to that very same function. At first this may seem like a never ending loop, or like a dog chasing its tail. It can never catch it. So too it seems our function will never finish. This might be true is some cases, but in practice we can check to see if a certain condition is true and in that case exit (return from) our function. The case in which we end our recursion is called a base case . Additionally, just as in a loop, we must change some value and incremently advance closer to our base case. For Example :- void myFunction( int counter) { if(counter == 0)      return; else        {        cout <<counter<<endl;        myFunction(--counter);        return;        } } Every recursion should have the following characteristi...

Understanding fclose

int fclose ( FILE * stream ); Close file Closes the file associated with the stream and disassociates it. All internal buffers associated with the stream are flushed: the content of any unwritten buffer is written and the content of any unread buffer is discarded. Even if the call fails, the stream passed as parameter will no longer be associated with the file. Parameters stream Pointer to a FILE object that specifies the stream to be closed. Return Value If the stream is successfully closed, a zero value is returned. On failure, EOF is returned. Example /* fclose example */ #include <stdio.h> int main () {   FILE * pFile;   pFile = fopen ("myfile.txt","wt");   fprintf (pFile, "fclose example");   fclose (pFile);   return 0; }

Vector functions

vector vect( float X, float Y, float Z ) Creates a new vector with the given components. float VSize( vector A ) Returns the euclidean size of the vector (the square root of the sum of the components squared). vector Normal( vector A ) Returns a vector of size 1.0, facing in the direction of the specified vector. Invert( out vector X, out vector Y, out vector Z ) Inverts a coordinate system specified by three axis vectors. vector VRand() Returns a uniformly distributed random vector. vector MirrorVectorByNormal( vector Vect, vector Normal ) Mirrors a vector about a specified normal vector.

Floating point functions

float Abs( float A ) Returns the absolute value of the number. float Sin( float A ) Returns the sine of the number expressed in radians. float Cos( float A ) Returns the cosine of the number expressed in radians. float Tan( float A ) Returns the tangent of the number expressed in radians. float ASin( float A ) Returns the inverse sine of the number expressed in radians. float ACos( float A ) Returns the inverse cosine of the number expressed in radians. float Atan( float A ) Returns the inverse tangent of the number expressed in radians. float Exp( float A ) Returns the constant "e" raised to the power of A. float Loge( float A ) Returns the log (to the base "e") of A. float Sqrt( float A ) Returns the square root of A. float Square( float A ) Returns the square of A = A*A. float FRand() Returns a random number from 0.0 to 1.0. float FMin( float A, float B ) Returns the minimum of two numbers. float FMax( float A, float B ) ...

Integer functions

int Rand( int Max ) Returns a random number from 0 to Max-1. int Min( int A, int B ) Returns the minimum of the two numbers. int Max( int A, int B ) Returns the maximum of the two numbers. int Clamp( int V, int A, int B ) Returns the first number clamped to the interval from A to B. Warning - Unlike the C or C++ equivalents, Min and Max work on integers. There is no warning for using them on floats - your numbers will just be silently rounded down! For floats, you need to use FMin and FMax.

Function Recursion

Function calls can be recursive. This means that the function can actually call itself. There are many situations where this is necessary or useful to cut down on the complexity of code needed to perform the desired action. For example, the following function computes the factorial of a number: // Function to compute the factorial of a number. function int Factorial( int Number ) { if( Number <= 0 )       return 1;     else       return Number * Factorial( Number - 1 ); }

Function Signature

The first line of the function declaration defines how the function is identified and addressed. This is called the function's signature . The signature specifies the name of the function, what values it can take in through function parameters, what (if any) return value it outputs, and any other pertinent information about the function through the use of function specifiers. The most basic function signature begins with the function keyword (special event functions begin event keyword instead). This is followed by the optional return type of the function, then the function name, and then the list of function parameters enclosed in parenthesis. From the example declaration above, the signature portion is: function byte GetTeam(Pawn PlayerPawn) This signature is for a function named GetTeam that takes in a reference to a Pawn and returns an byte value (which you can guess by the name of the function represents the team of the Pawn that was passed in). Functions can also...

Any Idea what is Local classes

A local class is declared within a function definition. Declarations in a local class can only use type names, enumerations, static variables from the enclosing scope, as well as external variables and functions. For example: int x;                         // global variable void f()                       // function definition {       static int y;            // static variable y can be used by                                // local class       int x;                   // auto variable x cannot be used by                                // local class       extern int g();   ...

Nested if and if-else statements

The if-else statement allows a choice to be made between two possible alternatives. Sometimes a choice must be made between more than two possibilities. For example the sign function in mathematics returns -1 if the argument is less than zero, returns +1 if the argument is greater than zero and returns zero if the argument is zero. The following C++ statement implements this function: if (x < 0)    sign = -1; else    if (x == 0)       sign = 0;    else       sign = 1; This is an if-else statement in which the statement following the else is itself an if-else statement. If x is less than zero then sign is set to -1, however if it is not less than zero the statement following the else is executed. In that case if x is equal to zero then sign is set to zero and otherwise it is set to 1.

Conditional operator ( ? )

The conditional operator evaluates an expression returning a value if that expression is true and a different one if the expression is evaluated as false. Its format is: condition ? result1 : result2 If condition is true the expression will return result1, if it is not it will return result2. 7==5 ? 4 : 3     // returns 3, since 7 is not equal to 5. 7==5+2 ? 4 : 3   // returns 4, since 7 is equal to 5+2. 5>3 ? a : b      // returns the value of a, since 5 is greater than 3. a>b ? a : b      // returns whichever is greater, a or b.  Conditional operator #include <iostream> using namespace std; int main () {   int a,b,c;   a=2;   b=7;   c = (a>b) ? a : b;   cout << c;   return 0; } In this example a was 2 and b was 7, so the expression being evaluated (a>b) was not true, thus the first value specified after the  question mark was discarded in favor of th...

Increase and decrease (++, --)

Shortening even more some expressions, the increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus: c++; c+=1; c=c+1; are all equivalent in its functionality: the three of them increase by one the value of c. In the early C compilers, the three previous expressions probably produced different executable code depending on which one was used. Nowadays, this type of code optimization is generally done automatically by the compiler, thus the three expressions should produce exactly the same executable code. A characteristic of this operator is that it can be used both as a prefix and as a suffix. That means that it can be written either before the variable identifier (++a) or after it (a++). Although in simple expressions like a++ or ++a both have exactly the same meaning, in other expressions in which the result of the increase or decrease operation is evaluate...

Fundamental data types

When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way. The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++. A byte can store a relatively small amount of data: one single character or a small integer (generally an integer between 0 and 255). In addition, the computer can manipulate more complex data types that come from grouping several bytes, such as long numbers or non-integer numbers. Here are some :-

Defined data types (typedef)

C++ allows the definition of our own types based on other existing data types. We can do this using the keyword typedef, whose format is: typedef existing_type new_type_name ; where existing_type is a C++ fundamental or compound type and new_type_name is the name for the new type we are defining. For example: typedef char C; typedef unsigned int WORD; typedef char * pChar; typedef char field [50];  In this case we have defined four data types: C, WORD, pChar and field as char, unsigned int, char* and char[50] respectively, that we could perfectly use in declarations later as any other valid type: C mychar, anotherchar, *ptc1; WORD myword; pChar ptc2; field name;  typedef does not create different types. It only creates synonyms of existing types. That means that the type of myword can be considered to be either WORD or unsigned int, since both are in fact the same type. typedef can be useful to define an alias for a type that is frequently used within ...