Monday, May 24, 2010

C++ question plzzzzzzzzz?

in c++ i wish to know how to write a program by using CLASS please how to write a programm by class i am learning C++ but i took holiday on that day when they said about CLASS we dont have backup classes because pragrams

C++ question plzzzzzzzzz?
Actually here its quite difficult to teach u but just first of all follow the concept of class then go to how we do it. Generally student dont go to the concept of class that just why we do it rather just structures. So, if you get all about OOPs and ecapsulations then it will be easy to understand how we do it and why we perform it so complexly when we can do it simply.
Reply:A very simple and very straight forward example of using a Class.





First you declare the class. (MyClass)


Specify that the code profiled is public (by using public: )


Define the prototype for your function within the class.


It carries two arguments, Dis1, and Dis2.





Close the class and define the function using the ( :: )


identifier. Be sure to specify the class description before


the function definition.:


int MyClass::MyCalculator(int Dis1, int Dis2)





In the main class create an instance of MyClass and


name it MC (MyClass MC)


Call the MyCalculator function from the MyClass instance


and display its results:





cout %26lt;%26lt; MC.MyCalculator(203, 492);








Here's the complete code:


Enjoy


- Hex














#include %26lt;iostream%26gt;


#include %26lt;conio.h%26gt; //for getch();








using namespace std;








class MyClass


{


public:


int MyCalculator(int Dis1, int Dis2);


};





int MyClass::MyCalculator(int Dis1, int Dis2)


{


return Dis1 + Dis2;


}





int main()


{


MyClass MC;


cout %26lt;%26lt; MC.MyCalculator(203, 492);





cout %26lt;%26lt; "\nPress any key to close...\n";


getch();


}
Reply:classes are similar to typedefs and structs...only they are capable of containing more (ie..contain member functions, constructors, destructors, etc...).





try this:





class myClass { // this is your class...


int aNumber;


char aChar;





int aFunction(){


cout%26lt;%26lt;"one"%26lt;%26lt;endl;


return 0;


}





};





int main(){ // this is your program's entry point...





myClass newClass; // newClass is the new instance..





newClass.aNumber = 1;


cout%26lt;%26lt;newClass.aNumber%26lt;%26lt;endl; // displays a number 1...





newClass.aChar = '1';


cout%26lt;%26lt;newClass.aNumber%26lt;%26lt;endl; //displays a string 1...





newClass.aFunction(); // displays the word one...





return 0;


}
Reply:its a long and most important topic of C++.so u better read it from ur book
Reply:Basically, classes are the basic "containers", or blueprints, for all objects in C++ and other object oriented languages such as Java and C#. I would strongly suggest you talk to a classmate or your professor about this, as it is a HUGE concept, and there is far too much info to post in a forum like this.
Reply:lets first look at the definition %26amp; example for a class


classes are the basic "containers", or blueprints, for all objects in C++ and other object oriented languages such as Java .A class in C++ is an encapsulation of data members and functions that manipulate the data. The class can also have some other important members which are architecturally important.





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


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 %26lt;%26lt; a.name %26lt;%26lt; ": " %26lt;%26lt; a.age %26lt;%26lt; endl;


cout %26lt;%26lt; b.name %26lt;%26lt; ": " %26lt;%26lt; b.age %26lt;%26lt; endl;


return 0;


}


but friend using ur text is the best way to go with c++ if u cant understand it..then ask ur friends or consult ur staff...CLASSES r important topics in c++,dont neglect them.ALL THE BEST
Reply:I recommend you do some serious reading and look at all of the practice code in your book. This is a critical concept in C++ and it's one that can be challenging to get your head around. You won't be able to move forward until you understand it, so plan to invest some serious time and attention. Once you understand it, it's pretty simple.





Here's a helpful link: http://www.cplusplus.com/doc/tutorial/cl...

nobile

No comments:

Post a Comment