C++ program to convert string into mm/dd/yyyy format
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 |
#include <iostream> #include <string.h> using namespace std; int main() { char date[11]; // mm/dd/yyyy int mm, dd, yyyy; cout<<"Enter date : "; cin>>date; if(strlen(date)<10 || strlen(date)>10 ) { cout<<"\n Error:Input should be in mm/dd/yyyy format"; } else if(strlen(date) == 10) { char m[3], d[3], y[5]; int i,j; for( i = 0; i<2; i++) m[i]=date[i]; for(i = 3, j=0; i<5; i++,j++){ d[j]=date[i]; } for(i = 6, j=0; i<10; i++, j++) y[j]=date[i]; mm = atoi(m); dd=atoi(d); yyyy = atoi(y); cout<<"\n Month : "<<mm; cout<<"\n Day : "<<dd; cout<<"\n Year : "<<yyyy; } else { cout<<"\n Error:Input should be in mm/dd/yyyy format"; } return 0; } |
C++ Program to check if number is Krishnamurthy
A Krishnamurthy number is a number whose sum of the factorial of digits is equal to the number itself. For example 145, 1! + 4! + 5! = 1 + (4*3*2*1) + (5*4*4*2*1) = 1 + 24 + 120 = 145. This C++ program takes input from user and check if it is Krishnamurthy. Program is divided into functions factorial(int) and isKrishnamurthy(int). For more details please run this code.
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 |
#include<iostream> using namespace std; //Function to calculate the factorial of any number int factorial(int n) { int fact = 1; while (n != 0) { fact = fact*n; n--; } return fact; } //function to Check if number is krishnamurthy void isKrishnamurthy(int n) { int sum = 0; int temp = n; while (temp != 0) { // calculate factorial of last digit // of temp and add it to sum sum += factorial(temp%10); // replace value of temp by temp/10 temp = temp/10; } // Check if number is krishnamurthy if(sum == n) cout << "Yes. Input no is a Krishnamurthy"; else cout << "No. Input no is not a Krishnamurthy"; } int main() { int n; cout<<"\n Input any no : "; cin>>n; isKrishnamurthy(n); return 0; } |
Finding Krishnamurthy numbers between 1 & 1000
We will use the above program only, but we will call isKrishnamurthy() function inside the for loop which runs from 1 to 1000
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 |
#include<iostream> using namespace std; //Function to calculate the factorial of any number int factorial(int n) { int fact = 1; while (n != 0) { fact = fact*n; n--; } return fact; } //function to Check if number is krishnamurthy void isKrishnamurthy(int n) { int sum = 0; int temp = n; while (temp != 0) { // calculate factorial of last digit // of temp and add it to sum sum += factorial(temp%10); // replace value of temp by temp/10 temp = temp/10; } // Check if number is krishnamurthy if(sum == n) cout <<n<<" - is a Krishnamurthy number"<<endl; } int main() { for(int i = 1; i<1000; i++) { isKrishnamurthy(i); } return 0; } |
Count the number of letters in the sentence
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 |
#include <iostream> using namespace std; struct sentence{ char s[500]; }; struct sentence obj; void countLetters(); int main() { cout << "Enter any sentence" << endl; cin.getline(obj.s,500); countLetters(); return 0; } void countLetters() { int i; int letters = 0; // letters for(i = 0; i<500; i++) { if( (obj.s[i]>='a' && obj.s[i]<='z') || (obj.s[i]<='A' && obj.s[i]>='Z')) { letters++; } } cout<<"\n No of letters in the sentence are : "<<letters; } |
Count the number of words in the sentence
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 |
#include <iostream> #include<string.h> using namespace std; struct sentence{ char s[500]; }; struct sentence obj; void numberofWords(); int main() { cout << "Enter any sentence" << endl; fgets( obj.s, 500, stdin ); numberofWords(); return 0; } void numberofWords() { int len = strlen(obj.s); cout<<"\n lenth is " <<len << ","; int i; int words = 0; i = 0; while(i<len) { if (obj.s[i++] == ' ') { words++; } } cout<<"words are : "<<words; |
Count the punctuation marks in any sentence
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include<iostream> using namespace std; int main(){ int countpunct = 0; char word; cout<<"\nEnter the Sentence: "; while ((word = getchar()) != EOF && word != '\n'){ if (word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){ countpunct++; } } cout<<"\nThe number of punctuation marsks is : "<<countpunct; } |
Program to find if string is Palindrome
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 |
#include <iostream> #include<string.h> using namespace std; struct sentence{ char string[50]; }; struct sentence obj; void function(); int main(){ cout << "Enter any string: "; cin >> obj.string; function(); return 0; } void function() { int i, length; int flag = 0; length = strlen(obj.string); for(i=0;i < length ;i++){ if(obj.string[i] != obj.string[length-i-1]){ flag = 1; break; } } if (flag) { cout << obj.string << " is not a palindrome" << endl; } else { cout << obj.string << " is a palindrome" << endl; } } |
Program to count letters in each word of sentence
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 |
#include <iostream> #define MAX_WORDS 10 using namespace std; struct st{ char text[100]; // to store string int cnt[MAX_WORDS]; //to store length of the words }; struct st obj; void printwords(); int main() { //reading string cout<<"Enter any string: "; fgets( obj.text, 100, stdin ); printwords(); return 0; } void printwords() { int len=0,i=0,j=0; while(1) { if(obj.text[i]==' ' || obj.text[i]=='\0') { //check NULL if(obj.text[i]=='\0') { if(len>0) { obj.cnt[j++]=len; len=0; } break; //terminate the loop } obj.cnt[j++]=len; len=0; } else { len++; } i++; } cout<<"\n Here is Words length : \n"; for(i=0;i<j;i++) { cout<<" "<<obj.cnt[i]; } } |
Count the vowels in the sentence
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 |
#include<iostream> #include<cstring> using namespace std; int vowelCount ( char *); int main(){ char word[80]; cout << "Enter a line: "; cin.getline (word, 80); cout << "\n Vowel count: " << vowelCount(word) << endl; return 0; } // returns the number of vowels in a char array int vowelCount ( char * pCh){ int vowels = 0; while(*pCh){ if(strspn(pCh, "aeiou")) vowels++; pCh++; } return vowels; } |
Is there a palindrome word the sentence
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 |
#include<iostream> using namespace std; struct sentence{ char str[50]; }; struct sentence obj; int countPalindrome(char str[50], int st, int ed); int main() { int pal = 0, len = 0, i, start = 0, end; cout<<"\n\n\t Enter any sentence : "; cin.getline (obj.str, 50); while(obj.str[len]!='\0') len++; len--; for(i=0;i<=len;i++) { if((obj.str[i] == ' ' && obj.str[i+1] != ' ') || i == len) { if(i == len) end = i; else end = i - 1; if( countPalindrome (obj.str, start, end ) ) pal++; start = end + 2; } } cout<<"\n\n\t the total number of palindrome are :" <<pal; return 0; } int countPalindrome(char str[50], int st, int ed) { int i, pal=0; for(i=0; i<=(ed-st)/2; i++) { if(str[st+i] == str[ed-i]) pal = 1; else { pal = 0; break; } } return pal; } |
Printing Reverse Rectangle using numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> using namespace std; int main(int argc, char *argv[]) { int count = 5; int i,j,k; for(i = 0 ; i<5; i++) { for(j=0; j<i; j++) cout<<" "; for(k=count; k>0; k-- ) cout<<k; count--; cout<<"\n"; } return 0; } |
Printing Triangle Pattern using numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include<iostream> using namespace std; int main(int argc, char *argv[]) { int count = 0; int i,j; int rows = 5; for(i = 0 ; i<rows; i++) { if(i<=(rows/2)) count++; else count--; for(j=1; j<=count; j++ ) cout<<j; cout<<"\n"; } return 0; } |
C++ Program to calculate area of Rectangle
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 |
#include<iostream> using namespace std; float area(float length, float width){ return (length*width); } int main() { float length, width, rec_area; cout<<"\n Enter Length : "; cin>>length; cout<<"\n Enter Width : "; cin>>width; rec_area = area(length, width); cout<<"\n Area of Rectangle is : "<< rec_area; return 0; } |
C++ Program of Recursion
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 |
#include<iostream> using namespace std; int factorial(int no) { int number = 1; if(no == 1) return 1; number = no * factorial(no -1); return number; } int main() { int no; cout<<"\n Enter any number : "; cin>>no; cout<<"\n Factorial is : "<< factorial(no); return 0; } |
C++ program to find avg of top 4 score
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 |
#include<iostream> using namespace std; void getScore(float &score) { cout<<"\n Enter test score : "; cin>>score; } float findLowest(float arr[]) { float lowest = arr[0]; for(int i = 1 ; i<5; i++) { if(lowest>arr[i]) { lowest = arr[i]; } } cout<<"\n Lowest is : "<<lowest; return lowest; } void calcAverage(float arr[]){ float lowest = findLowest(arr); float avg = 0; for(int i = 0; i< 5; i++) { if(arr[i] != lowest) avg = avg + arr[i]; } avg = avg/4; cout<<"\n Average is : "<< avg; } int main() { float arr[5]; for(int i = 0; i< 5; i++) { getScore(arr[i]); } calcAverage(arr); return 0; } |
C++ program to calulate seconds in given time
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 |
#include<iostream> using namespace std; unsigned long int calcualteSeconds(unsigned int hours, unsigned int minutes, unsigned int seconds) { unsigned long calSec = 0; calSec = hours * 60 * 60; calSec = calSec + minutes * 60; calSec = calSec + seconds; return calSec; } int main() { unsigned int hours, minutes, seconds; unsigned long int CalculateSeconds = 0; cout<<"\n Enter hours : "; cin>>hours; cout<<"\n Enter minutes : "; cin>>minutes; cout<<"\n Enter seconds : "; cin>>seconds; CalculateSeconds = calcualteSeconds(hours, minutes, seconds); cout<<"\n No of Seconds are : "<< CalculateSeconds; return 0; } |
C++ program to check if sequence of nos are even or odd
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 |
#include<iostream> using namespace std; int even(int no) { if(no%2 == 0) return 1; else return 0; } int main() { int no; int result = 0; cout<<"\n Enter any no: "; cin>>no; result = even(no); if(result == 1) cout<<"\n No is even"; else cout<<"\n No is odd"; cout<<"\n Enter any no: "; cin>>no; result = even(no); if(result == 1) cout<<"\n No is even"; else cout<<"\n No is odd"; cout<<"\n Enter any no: "; cin>>no; result = even(no); if(result == 1) cout<<"\n No is even"; else cout<<"\n No is odd"; cout<<"\n Enter any no: "; cin>>no; result = even(no); if(result == 1) cout<<"\n No is even"; else cout<<"\n No is odd"; return 0; } |
C++ program to check if number is prime using function
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 |
#include<iostream> using namespace std; void isPrime(int no) { bool isPrime = true; for(int i = 2; i <= no / 2; ++i) { if(no % i == 0) { isPrime = false; break; } } if (isPrime) cout << "This is a prime number"; else cout << "This is not a prime number"; } int main() { int no; cout<<"\n Enter any no : "; cin>>no; isPrime(no); return 0; } |
C++ program to find power of any number without using pow() function
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 |
#include<iostream> using namespace std; unsigned long int integerPower(int base, unsigned int power) { unsigned long int val= base; for(unsigned long int i = 0; i< power-1; i ++) { val = val * base; } return val; } int main() { int base; unsigned int power; unsigned long int val = 0; cout<<"\n Enter base (non zero) : "; cin>>base; cout<<"\n Enter power (positive) : "; cin>>power; val = integerPower(base, power); cout<<"\n Result is : "<< val; return 0; } |
C++ program to calculate sales of 6 months
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 |
#include<iostream> #include<string> using namespace std; void input(string *ptrMonthNamme, string *ptrSalesFig ) { for(int i = 0; i<6; i++) { cout<<"\n\n Enter the sales figure of "<<*ptrMonthNamme<<" : "; cin>>*ptrSalesFig; cout<<"\t The Memory location: "<<&ptrSalesFig; ptrSalesFig++; ptrMonthNamme++; } } double totaling(string *ptrSalesFig) { double sales = 0; for(int i = 0; i<6; i++) { double temp = 0; string s = *ptrSalesFig; const char *ctemp = s.c_str(); ctemp++; temp = atof(ctemp); sales += temp; ptrSalesFig++; } return sales; } void listing(string *ptrMonthNamme, string * ptrSalesFig) { for(int i = 0; i<6; i++) { cout<<"\n Sales for "<<*ptrMonthNamme<<" : "<<*ptrSalesFig; ptrMonthNamme++; ptrSalesFig++; } } int main() { string monthsName[] ={"Jan", "Feb", "Mar", "Apr", "May", "Jun"}; string salesFigures[6]; input(monthsName, salesFigures); cout<<" \n\n Total sales is : "<<totaling(salesFigures); listing(monthsName, salesFigures); return 0; } |
C++ Program on Drinking/Vending Machine
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
#include<iostream> #include<stdlib.h> #include<string.h> #include <iomanip> using namespace std; struct softdrink{ char name[20]; float price; unsigned quantity; }; int main() { softdrink drink[5]; strcpy(drink[0].name,"Cola"); drink[0].price=0.75; drink[0].quantity=20; strcpy(drink[1].name,"Root Beer"); drink[1].price=0.75; drink[1].quantity=20; strcpy(drink[2].name,"Lemon Lime"); drink[2].price=0.75; drink[2].quantity=20; strcpy(drink[3].name,"Grape Soda"); drink[3].price=0.80; drink[3].quantity=20; strcpy(drink[4].name,"Cream Soda"); drink[4].price=0.80; drink[4].quantity=20; std::cout << std::fixed; std::cout << std::setprecision(4); int choice = 1; while(choice != 6){ cout<<"\n 1) "<<drink[0].name<<"\t\t"<<drink[0].price<<"\t("<<drink[0].quantity<<") remaining"; cout<<"\n 2) "<<drink[1].name<<"\t\t"<<drink[1].price<<"\t("<<drink[1].quantity<<") remaining"; cout<<"\n 3) "<<drink[2].name<<"\t\t"<<drink[2].price<<"\t("<<drink[2].quantity<<") remaining"; cout<<"\n 4) "<<drink[3].name<<"\t\t"<<drink[3].price<<"\t("<<drink[3].quantity<<") remaining"; cout<<"\n 5) "<<drink[4].name<<"\t\t"<<drink[4].price<<"\t("<<drink[4].quantity<<") remaining"; cout<<"\n 6) Leave the drink machine \n\n"; cout<<"\n Choose one:"; cin>>choice; if(choice >=1 && choice <=5) { if(drink[choice-1].quantity == 0) { cout<<"\n No more " << drink[choice-1].name <<" Available .."; getchar(); getchar();continue; } } if(choice == 6) cout<<"Thank for using it !!"; else if(choice <= 5) { float money; cout<<"\n Enter any amount of money: "; cin>>money; float price; if(choice>=1 && choice <=3) { price = .75; if((money <price)){ cout<<"\n Enter sufficient amount "; getchar(); getchar(); continue; } } else if(choice ==4 || choice ==5) { price = .80; if((money <price)){ cout<<"\n Enter sufficient amount "; getchar();getchar(); continue; } } cout<<"\n Thum, thum, thum, splat !"; cout<<"\n Enjoy your beverage "; cout<<"\n\n Change calcualted : "<< money-price; cout<<"\n You change, "<<money-price<<" just droped into the Change Dispenser."; drink[choice-1].quantity = drink[choice-1].quantity - 1; cout<<"\n There are "<< drink[choice-1].quantity <<" drinks of that type left"; getchar(); getchar(); } else { cout<<"\n Warning : Invalid Choice "; } } return 0; } |
How to initialize const variables of any class in C++
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 |
#include <iostream> #include<math.h> using namespace std; class circle{ double r; const double pi; public: circle(double r) : pi(3.14159){ this->r=r; } double area (){ return pi*(pow(r,2)); } double circumference (){ return 2*pi*r; } }; int main(){ circle c=circle(6.70); cout<<"area of circle :"<<c.area()<<endl; cout<<"circumference of circle :"<<c.circumference ()<<endl; return 0; } |
C++ program that reads in the lines from a text file & displays it
Write a program that reads in the lines from a text file and displays them with line numbers and a colon. Limit the display to 24 lines at a time per the instructions. The output of the line numbers will be right aligned as shown below. The program may be handled in main.
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 |
#include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; int main(int argc, char** argv) { string filename; cout<<"\n Enter the file name : "; cin>>filename; ifstream readFile(filename.c_str()); string line; unsigned long int lineNo = 1; while(getline(readFile, line)) { cout<<"\n"<<setw(3)<<lineNo<<":" << line; if(lineNo%24 ==0) { cout<<"\n\t Press ENTER to continue..."; getchar();getchar(); } lineNo++; } return 0; } |
C++ program to convert string into enum
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 |
#include <iostream> #include <string> enum Days {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; Days convert(const std::string& str) { if(str == "Monday") return Monday; else if(str == "Tuesday") return Tuesday; else if(str == "Wednesday") return Wednesday; else if(str == "Thursday") return Thursday; else if(str == "Friday") return Friday; else if(str == "Saturday") return Saturday; else if(str == "Sunday") return Sunday; } int main() { std::string str; std::cout<<"\n Enter any day in words (like Sunday, Monday) : "; std::cin >> str; Days day = convert(str); switch (day) { case Monday: std::cout << "This is Monday" << std::endl; break; case Tuesday: std::cout << "This is Tuesday" << std::endl; break; case Wednesday: std::cout << "This is Wednesday" << std::endl; break; case Thursday: std::cout << "This is Thursday" << std::endl; break; case Friday: std::cout << "This is Friday" << std::endl; break; case Saturday: std::cout << "This is Saturday" << std::endl; break; case Sunday: std::cout << "This is Sunday" << std::endl; break; default: std::cout<<"\n\n Inavlid Input"; } return 0; } |
C++ program to find sum of n numbers using function
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 |
#include <stdio.h> #include <conio.h> int sum (int number); int main() { int no; printf("\n How many numbers? : "); scanf("%d", &no); printf("\n Sum of the %d numbers is : %d ",no,sum(no)); return 0; } int sum(int number){ int i, addition=0; for(i=1; i<=number; i++){ addition += i; } return addition; } |
C++ program on inheritance and virtual functions
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#include<string.h> #include<iostream> using namespace std; class Ship{ public: char name[50]; char yearBuilt[10]; Ship(){ //nothing to do } Ship(char *name, char *yearBuilt){ strcpy(this->name, name); strcpy(this->yearBuilt, yearBuilt); } virtual void print(){ cout<<" Name: "<<name; cout<<"\n Year built: "<<yearBuilt<<endl; } }; class CruiseShip: public Ship{ private: int passenger; public: CruiseShip(char *name, char *yearBuilt, int passenger){ strcpy(this->name, name); strcpy(this->yearBuilt, yearBuilt); this->passenger = passenger; } void print(){ cout<<" Name: "<<name; cout<<"\n Maximum Passenger: "<<passenger<<endl; } }; class CargoShip: public Ship{ private: long int capacity; public: CargoShip(char *name, char *yearBuilt, long int capacity){ strcpy(this->name, name); strcpy(this->yearBuilt, yearBuilt); this->capacity = capacity; } void print(){ cout<<" Name: "<<name; cout<<"\n Cargo capacity: "<<capacity<< "tons"<<endl; } }; int main() { // Create an array of Ship pointers, initialized // with the addresses of 3 dynamically allocated objects. Ship *ships[3] = { new Ship("Lolipop", "1960"), new CruiseShip("Disney Magic", "1998", 2400), new CargoShip("Black Pearl", "1800", 50000) }; // Call each object's print function. for (int index=0; index < 3; index++) { ships[index]->print(); cout << "----------------------------\n"; } return 0; } |