Posts

Program to find Addition of "n" numbers useing for loop

Program :- #include<iostream.h> void main() { int total = 0; for(int i=1;i<11;i++)  \\ 11 number i used for example { total  = total + i; } cout<<"sum of first 10 numbers is :-"<<total;  \\10 number i used to get output of }                                                                            \\sum of 10 numbers Output :- sum of first 10 numbers is:- 55 It is one of the great programs for example u can just change the numbers and get the output of any number you want Its like 1+2+3+4 =10 This is for sum of 4 numbers. 

Deciding the day of week using switch-case construct in C++

Program :- #include<iostream.h> void main() { int day: cout<<"Enter the number in between of 1 to 7 to get corresponding day :"; cin>>day; switch(day) {       case 1: cout<<"Today is Sunday";       break;       case 2: cout<<"Today is Monday";       break;       case 3: cout<<"Today is Tuesday";       break;       case 4: cout<<"Today is Wednesday";       break;       case 5: cout<<"Today is Thursday";       break;       case 6: cout<<"Today is Friday";       break;          case 7: cout<<"Today is Saturday";       break;        default : cout<<"Not a valid day";       break; } } Output :- Enter the numberin be...

A program illustrating how to pass reference to object as a function argument in C++

 A program illustrating how to pass reference to object as a function argument Program Explanation : Here in the putdata() function we have passed reference to object to modifiy the data member from within the member function via it's Object. Program :- #include<iostream,h> class ABC { public: int data; void getdata() { cout<<"Enter number:"; cin<<data; cout<<"You entered:"<<data; } void putdata(ABC &obj) { obj.data=-obj.data; cout<<"Now number is changed to:"<<obj.data; } }; void main() { ABC obj1; obj1.getdata(); obj1.putdata(obj1); } Output is : Entered number : 10 You entered : 10 Now number is changed to :    -10

Accessing members of a class using pointers (pointers to object members of a class) in C++

Accessing members of a class using pointers (pointers to object members of a class) Pointers to Object members We have seen use of pointers, to point to a variable. Pointers can also be used to point object (members of a class): Program :- #include<iostream.h> class ABC { public: int data; void getdata() { cout<<"Enter data:"; cin>>data; cout<<"The number entered is :"<<data; } }; void main() { ABC obj; int *p; obj.data=3 p = &obj.data; cout<<*p; ABC *q; q =&obj.data; q->getdata(); } Output :- 3 Entered the number : 5 The number entered is : 5 

Illustrates invoking class members (data and function) via instance (objects) in C++

Program :- #include<iostream.h> class ABC { public: int i; void display() { cout<<"Invoking member function through object"; } }; void main() { ABC obj; obj.i=4; cout<<obj.i; obj.display(); } Output :- 4 Invking member functon through object

What are OBJECT in C++ ?

OBJECT in C++ Objects are autonomous entities of the class; they are instances of the class and belong to class. Traditionally, code and data have been kept apart but in o-o (object-oriented) programming, code and data are meregd into a single indivisible thing and object. Thus methods are normally invoked using objects.