C++ OOP program to print stars in a square using classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#include<iostream> // header file it provide input output service to c++ programs. using namespace std; // namespace class a // class is created with " a" name { // class "a " is starting from here protected: // Protected members are accessible within the class and within its derived/child classes. int n,i,j; // int is datatype and n,i,j are variables of integer type and this is protected.these are accessible within the class and within its derived/child classes. public: // Public members are accessible within the class and also outside the class. int out() // out is a function and this is public that can be accessible outside the class. { // out function is started from here cout<<"enter number"; // this line ask the user to enter number cin>>n; // this line used to take input from user for(i=1;i<=n;i++) // for loop is started with value of i is equal to 1 and loop will execute until value of i is less than n and loop will increment if condition is true { // started nested loop for( j=1;j<=n;j++) // this loop is started from 1 j is equal to 1 and loop will execute until the value of j is less than n and loop will increment if condition is true { // started of nested for loop if(i==1||i==n||j==1||j==n) // in nested loop using if statement in this statement if i is eqaul to 1 or i is equal to n ( n that is entered by user) or j is equal to 1 or n then this condition will true { // starting of if condition cout<<"*"; // if,if condition is true then this statement will display * on the that places where if statement is true } // ending of cout statement else // else condition is started from here { // else condition started cout<<" "; // if , if condition will false then else program move to the else condition and this statement will show space on that places where if statement is not true } // ending of else condition } // ending of if statement cout<<endl; // this statement is used to enter space } // ending of for loop } // ending of out function }; // ending of class int main() // starting main function // main function is started { a c; // a is name of class and c is object of class a c.out(); // out function is a property of object c and c is a object of class a } // ending of main function |