What are the disadvantage of UML?
Developing/coding/modeling anything while satisfying any specific standards will usually decrease the technical performance (efficiency) of your end product (software). Standards (like UML standards) are there to make creating the end product a more rapid process, and to make it more manageable to work with other people / reuse existing software. A 'capable programmer/developer' can almost always create end products that perform better by explicitly creating the program for its purpose, disregarding standard routines.
However, you almost always DO want to follow standards (not just UML), because the performance loss is negligible for most products.
#include<iostream>
#include<sstream>
int main()
{
unsigned num;
while (true)
{
std::cout << "Enter a number: ";
std::string input;
std::cin >> input;
std::stringstream ss;
ss << input;
if (ss>>num) break;
std::cerr << "Invalid input.\n";
}
for (int i=0; i<num; ++i)
std::cout << "Well done!\n";
}
What will be the Flowchart for adding all elements in an array in c plus plus?
It is not possible to show a flowchart in this website -- it is text only. The algorithm can be summarised as follows:
int sum(std::array<int>& a)
{
int sum = 0; // initialise the return value
for (auto i : a) // for each value in the array
sum += i; // increment the sum by the value
return sum; // return the sum
}
Why would a function prototype contain a parameter type declaration such as double and?
declaration of functions show takes one argument of double as
call by reference
exmp:
void show(double &);
double x=10;
show(x);
void show(double & z)
{
z=z+10;
}
How do you write a C plus plus code for first fit worst fit best fit algorithms?
The following program shows one method of implementing first-fit, best-fit and worst-fit algorithms. There's a lot of code but it can be broken down into 4 major classes: process, processes, partition and partitions. In the real-world, the partition class would allocate memory to itself and assign one or more processes to that memory (keeping track of which process is where). Likewise, the process class would represent an actual process. However, to keep things simple, we're just modelling these objects to show how the algorithms work in terms of allocating a process to a partition based upon the size of the process and the available bytes in a partition.
The process class simply stores an ID and the size of the process (in bytes). The processes class is really nothing more than a wrapper for an embedded vector of processes. Similarly, the partition class holds an ID, the size of the partition and an embedded processes class to keep track of which processes it holds. The partitions class is also nothing more than a wrapper for an embedded vector of partitions.
In the main function we can see how these classes are used. First, 5 partitions are allocated sizes of 4096, 2048, 1024, 512 and 256 bytes. In the real-world partitions would be equal size, however we can imagine that they were all 4096 bytes to begin with and that these sizes represent what is currently available. We then create 10 processes from 2048 down to 4 bytes in size. We then randomise the order of both collections to give a more realistic example (the IDs tell us the order in which they were actually created).
The key to allocating the processes to the partitions is the processes::allocate function, where we pass a reference to the processes to be allocated, along with the method (first, best or worst).
With first-fit, we simply take each process in turn and allocate it to the first partition that is large enough. Since the processes and partitions were randomised to begin with, they could be placed in any order. However, note that the largest process (process 1 with 2048 bytes) can only be placed in partition 1 (4096 bytes) or partition 2 (2048 bytes), but can only be placed in partition 2 if there no other processes occupying that partition. Partition 1 is capable of holding all 10 processes at once, so there should never be the situation where a process cannot be allocated (in the real-world it can happen, in which case you would need to try and add a new partition).
With best-fit, we're looking for the partition that leaves the least free bytes for each process. To achieve this we sort the processes in descending order of size. We also sort the partitions in ascending order of size before each process is allocated to a partition. By sorting the collections in this manner, we can re-use the first-fit algorithm, which will always finds the smallest partition that will accept the process.
With worst-fit we do the same thing as we did with best-fit, except we reverse the order. Processes are sorted in ascending order of size and partitions in descending order of size. Again, this allows us to use the first-fit algorithm to find the largest partition that will accept the process.
In other words, we use the same algorithm for all three, the only difference being the order of the processes and partitions.
In the real-world, of course, we'd allocate just one process at a time rather than a collection of processes all at once. Thus the only difference with best-fit and worst-fit is that we do not need to sort the processes at all, but we must still sort the partitions based upon the current available bytes (but not for first-fit). We achieve this by totalling the size of the current processes within that partition and subtract that sum from the partition size. However, in the real world processes may be deallocated in any sequence, which will leave non-contiguous gaps in the partition. We get around that problem by allocating sub-partitions to those gaps, thus every partition is really a tree of sub-partitions that starts out with one node (the root). In order to locate the best or worst fit we must 'flatten' the trees by creating a vector for all the nodes and sub-nodes and then sort that vector by the size of each node. This is well beyond the scope of the answer of course, however the program below contains enough information to be able to apply first, best and worst-fit to any vector, regardless of its source.
Program code
#include
#include
#include
#include
#include
// forward declarations
class process_class;
class processes_class;
class partition_class;
class partitions_class;
std::ostream& operator<< (std::ostream& os, const process_class& process);
std::ostream& operator<< (std::ostream& os, const processes_class& processes);
std::ostream& operator<< (std::ostream& os, const partition_class& partition);
std::ostream& operator<< (std::ostream& os, const partitions_class& partitions);
// enumerations
enum fitness {
first = 0,
best = 1,
worst = 2
};
// the process class
class process_class
{
private:
static unsigned s_id;
unsigned m_id;
unsigned m_bytes;
public:
process_class (unsigned bytes): m_bytes (bytes), m_id (++s_id) {}
bool operator< (const process_class& rhs) { return (m_bytes < rhs.m_bytes); }
unsigned get_id () const { return (m_id); }
unsigned get_bytes () const { return (m_bytes); }
};
// initialise static member
unsigned process_class::s_id = 0;
// insertion operator overload
std::ostream& operator<< (std::ostream& os, const process_class& process)
{
os << "Process " << std::setw (2) << process.get_id() << " (" << std::setw (4) << process.get_bytes() << " bytes)";
return (os);
}
// the processes class
class processes_class
{
private:
std::vector
static bool compare (process_class* a, process_class* b) {
return (a->get_bytes() < b->get_bytes()); }
public:
processes_class()
: m_vect () {}
processes_class (const processes_class& other)
: m_vect (other.m_vect) {}
processes_class& operator= (const processes_class& rhs)
{
m_vect = rhs.m_vect;
return (*this);
}
unsigned get_bytes() const
{
unsigned bytes = 0;
for (std::vector
bytes += (*it)->get_bytes();
return (bytes);
}
unsigned size() const {return (m_vect.size()); }
void sort_up() { std::sort (m_vect.begin(), m_vect.end(), &processes_class::compare); }
void sort_down() { std::sort (m_vect.rbegin(), m_vect.rend(), &processes_class::compare); }
void unsort()
{
unsigned size = m_vect.size();
for (unsigned i=1; i { unsigned rnd = rand() % --size + i; std::swap (*m_vect[i-1], *m_vect[rnd]); } } void swap (processes_class& rhs) { std::swap (m_vect, rhs.m_vect); }; // vector iterators and other helpers std::vector std::vector std::vector std::vector std::vector std::vector std::vector std::vector bool empty() { return (m_vect.empty()); }; void pop_back() { m_vect.pop_back(); } std::vector std::vector void push_back(process_class* process) { m_vect.push_back (process); } }; // insertion operator overload std::ostream& operator<<(std::ostream& os, const processes_class& processes) { os << "Processes: " << processes.size() << '\n' << std::endl; for (std::vector current_process!=processes.end(); ++current_process) { const process_class& process = **current_process; os << process << std::endl; } return (os); } // the partition class class partition_class { private: static unsigned s_id; unsigned m_id; unsigned m_bytes; processes_class m_processes; void swap (partition_class& rhs) { std::swap (m_id, rhs.m_id); std::swap (m_bytes, rhs.m_bytes); m_processes.swap (rhs.m_processes); } public: partition_class (unsigned bytes) : m_bytes(bytes), m_id(++s_id) {} partition_class(const partition_class& other) : m_bytes (other.m_bytes), m_id (other.m_id), m_processes (other.m_processes) {} partition_class& operator= (const partition_class& rhs) { m_bytes = rhs.m_bytes; m_id = rhs.m_id; m_processes = rhs.m_processes; return (*this); } unsigned get_id() const { return (m_id); } unsigned get_bytes() const { return (m_bytes - m_processes.get_bytes()); } void allocate(process_class& process) { std::cout << process << " allocated to " << *this; m_processes.push_back (&process); std::cout << " leaving " << std::setw(4) << get_bytes() << " bytes free" << std::endl; } void deallocate_all() { while (!m_processes.empty()) m_processes.pop_back(); } }; // initialise static member unsigned partition_class::s_id = 0; // insertion operator overload std::ostream& operator<< (std::ostream& os, const partition_class& partition) { os << "Partition " << partition.get_id() << " (" << std::setw(4) << partition.get_bytes() << " bytes)"; return (os); } // forward declarations class partitions_class; std::ostream& operator<< (std::ostream& os, const partitions_class& partition); // the partitions class class partitions_class { private: std::vector partitions_class (const partitions_class&); // disabled partitions_class& operator= (const partitions_class&); // disabled static bool compare (partition_class& a, partition_class& b) { return (a.get_bytes() < b.get_bytes()); } public: partitions_class () {} void sort_up() { std::sort (m_vect.begin(), m_vect.end(), &partitions_class::compare); } void sort_down() { std::sort (m_vect.rbegin(), m_vect.rend(), compare); } void unsort() { unsigned size = m_vect.size(); for (unsigned i=1; i { unsigned rnd = rand() % --size + i; std::swap (m_vect[i-1], m_vect[rnd]); } } void allocate (processes_class& processes, fitness fit) { switch(fit) { case (first): std::cout << "First Fit\n" << std::endl; break; case (best): std::cout << "Best Fit\n" << std::endl; processes.sort_down(); break; case (worst): std::cout << "Worst Fit\n" << std::endl; processes.sort_up(); break; } // loop through processes for (std::vector { // dereference the current process process_class& process = **current_process; // initialise a flag bool flag = false; // for best and worst fit only, sort the partitions according to remaining bytes switch(fit) { case (best): sort_up(); break; case (worst): sort_down(); break; } // loop through partitions for (std::vector { // dereference the current partition partition_class& partition = *current_partition; // will the process fit within the current partition? if (process.get_bytes() <= partition.get_bytes()) { // yes! partition.allocate (process); flag = true; break; } } // unable to allocate the current process if (!flag) std::cout << process << " cannot be allocated!" << std::endl; } std::cout << std::endl; } void deallocate() { for (std::vector current_partition->deallocate_all(); } // vector iterators and other helpers std::vector std::vector std::vector std::vector std::vector std::vector std::vector std::vector unsigned size() const { return (m_vect.size()); } void push_back (partition_class partition) { m_vect.push_back (partition); } }; // insertion operator overload std::ostream& operator<< (std::ostream& os, const partitions_class& partitions) { os << "Memory partitions: " << partitions.size() << '\n' << std::endl; for (std::vector current_partition!=partitions.end(); ++current_partition) { const partition_class& partition = *current_partition; os << partition << std::endl; } return (os); } int main() { // seed the random number generator srand ((unsigned) time (NULL)); // instantiate partitions and processes partitions_class partitions; processes_class processes; // assign decreasing memory sizes to 5 partitions unsigned memory = 8192; for (unsigned partition=0; partition<5; ++partition) partitions.push_back (partition_class (memory>>=1)); // assign decreasing memory sizes to 10 processes memory = 4096; for (unsigned proc=0; proc<10; ++proc) processes.push_back (new process_class (memory>>=1)); // unsort (randomise) the partitions and processes partitions.unsort(); processes.unsort(); // print them std::cout << partitions << std::endl; std::cout << processes << std::endl; // exercise the algorithms partitions.allocate (processes, first); partitions.deallocate (); partitions.allocate (processes, best); partitions.deallocate (); partitions.allocate (processes, worst); partitions.deallocate (); // tidy up while (!processes.empty()) { delete (processes.back()); processes.pop_back(); } std::cout << std::endl; } Example output Memory partitions: 5 Partition 5 ( 256 bytes) Partition 3 (1024 bytes) Partition 2 (2048 bytes) Partition 4 ( 512 bytes) Partition 1 (4096 bytes) Processes: 10 Process 3 ( 512 bytes) Process 6 ( 64 bytes) Process 8 ( 16 bytes) Process 2 (1024 bytes) Process 10 ( 4 bytes) Process 4 ( 256 bytes) Process 7 ( 32 bytes) Process 1 (2048 bytes) Process 9 ( 8 bytes) Process 5 ( 128 bytes) First Fit Process 3 ( 512 bytes) allocated to Partition 3 (1024 bytes) leaving 512 bytes free Process 6 ( 64 bytes) allocated to Partition 5 ( 256 bytes) leaving 192 bytes free Process 8 ( 16 bytes) allocated to Partition 5 ( 192 bytes) leaving 176 bytes free Process 2 (1024 bytes) allocated to Partition 2 (2048 bytes) leaving 1024 bytes free Process 10 ( 4 bytes) allocated to Partition 5 ( 176 bytes) leaving 172 bytes free Process 4 ( 256 bytes) allocated to Partition 3 ( 512 bytes) leaving 256 bytes free Process 7 ( 32 bytes) allocated to Partition 5 ( 172 bytes) leaving 140 bytes free Process 1 (2048 bytes) allocated to Partition 1 (4096 bytes) leaving 2048 bytes free Process 9 ( 8 bytes) allocated to Partition 5 ( 140 bytes) leaving 132 bytes free Process 5 ( 128 bytes) allocated to Partition 5 ( 132 bytes) leaving 4 bytes free Best Fit Process 1 (2048 bytes) allocated to Partition 2 (2048 bytes) leaving 0 bytes free Process 2 (1024 bytes) allocated to Partition 3 (1024 bytes) leaving 0 bytes free Process 3 ( 512 bytes) allocated to Partition 4 ( 512 bytes) leaving 0 bytes free Process 4 ( 256 bytes) allocated to Partition 5 ( 256 bytes) leaving 0 bytes free Process 5 ( 128 bytes) allocated to Partition 1 (4096 bytes) leaving 3968 bytes free Process 6 ( 64 bytes) allocated to Partition 1 (3968 bytes) leaving 3904 bytes free Process 7 ( 32 bytes) allocated to Partition 1 (3904 bytes) leaving 3872 bytes free Process 8 ( 16 bytes) allocated to Partition 1 (3872 bytes) leaving 3856 bytes free Process 9 ( 8 bytes) allocated to Partition 1 (3856 bytes) leaving 3848 bytes free Process 10 ( 4 bytes) allocated to Partition 1 (3848 bytes) leaving 3844 bytes free Worst Fit Process 10 ( 4 bytes) allocated to Partition 1 (4096 bytes) leaving 4092 bytes free Process 9 ( 8 bytes) allocated to Partition 1 (4092 bytes) leaving 4084 bytes free Process 8 ( 16 bytes) allocated to Partition 1 (4084 bytes) leaving 4068 bytes free Process 7 ( 32 bytes) allocated to Partition 1 (4068 bytes) leaving 4036 bytes free Process 6 ( 64 bytes) allocated to Partition 1 (4036 bytes) leaving 3972 bytes free Process 5 ( 128 bytes) allocated to Partition 1 (3972 bytes) leaving 3844 bytes free Process 4 ( 256 bytes) allocated to Partition 1 (3844 bytes) leaving 3588 bytes free Process 3 ( 512 bytes) allocated to Partition 1 (3588 bytes) leaving 3076 bytes free Process 2 (1024 bytes) allocated to Partition 1 (3076 bytes) leaving 2052 bytes free Process 1 (2048 bytes) allocated to Partition 1 (2052 bytes) leaving 4 bytes free
Why dafault constructor is necessary in c programming?
It is not necessary (nor possible) in C programming.
What do you mean by a static function?
In Java, a static function is not attached to a particular object, but rather to the whole class.
For example, say we have the following class:
class Song {
String song;
void printSong() {
System.out.println(song);
}
static void printStaticSong() {
System.out.println("This is a static song. It has no tune.");
}
}
The static function Song.printStaticSong() can be accessed without creating an instance of the Song class (using the "new" keyword)
However it cannot access the members of Song (such as song), since it is not an instance of that object.
#include<iostream> #include<string> #include<sstream> #include<vector> #include<conio.h> // required by getch() std::string input_string (std::string prompt) { while (true) { std::string input; std::cout << prompt; std::getline (std::cin, input); if (input.size()) return input; std::cout << "Invalid input. Empty strings are not permitted.\n"; } } size_t input_unsigned (std::string prompt) { while (true) { std::string input = input_string (prompt); std::stringstream ss; ss << input; size_t s; if (ss>>s && s) return s; std::cout << "Invalid input. Values must be non-zero.\n"; } } void string_length() { std::string str = input_string ("Enter the string you wish to know the length of:\n"); std::cout << "The string is " << str.size() << " characters long.\n" << std::endl; } void string_compare() { std::pair<std::string, std::string> strings; strings.first = input_string ("Enter the first string you wish to compare:\n"); strings.second = input_string ("Enter the second string you wish to compare:\n"); int compare = strings.first.compare (strings.second); std::cout << "The result of the camprison is: " << compare << '\n' << std::endl; } void string_reverse() { std::string str = input_string ("Enter the string you wish to reverse:\n"); for (size_t f=0, r=str.size()-1; f<r; ++f, --r) { char t = str[f]; str[f] = str[r]; str[r] = t; } std::cout << "The reversed string is:\n" << str << '\n' << std::endl; } void string_substring() { std::string str = input_string ("Enter a string:\n"); size_t pos = input_unsigned ("Enter the offset of the substring:\n"); size_t len = input_unsigned ("Enter the length of the substring:\n"); std::cout << "The substring is: " << str.substr(pos,len) << '\n' << std::endl; } void string_concatenation() { std::pair<std::string, std::string> strings; strings.first = input_string ("Enter the first string you wish to concatenate:\n"); strings.second = input_string ("Enter the second string you wish to concatenate:\n"); std::string concat = strings.first + strings.second; std::cout << "The result of the concatenation is: " << concat << '\n' << std::endl; } int main() { while (true) { std::cout << "MAIN MENU\n"; std::cout << "=========\n" << std::endl; std::cout << "1 - String length\n"; std::cout << "2 - String compare\n"; std::cout << "3 - String reverse\n"; std::cout << "4 - String substring\n"; std::cout << "5 - String concatenation\n"; std::cout << "X - Exit program\n"; #pragma warning(disable : 4996) switch ((char) getch()) { case ('1'): string_length(); break; case ('2'): string_compare(); break; case ('3'): string_reverse(); break; case ('4'): string_substring(); break; case ('5'): string_concatenation(); break; case ('x'): case ('X'): return 0; } } }
What is the algorithm using c to delete duplicate elements from an array?
To detect the duplicate, you will have to write a nested loop that compares each element with all the previous elements.
To actually delete the duplicate, once you find it, you have to move over all the elements after the duplicate. If the order of the elements doesn't matter, it is faster to just move the LAST array element, overwriting the duplicate element. Use a variable to keep track how many elements of the array are "usable". For example, if your array had 10 elements, and you delete 1, the array size will still be 10... but (after moving the elements over) only 9 of those elements have useful information.
Add two matrices by using arrays and functions?
#include
#include
void main()
{ int a[10][10],b[10][10],c[10][10];
printf("\nEnter the Row:");
scanf("%d",&r);
printf("\nEnter the Columns:");
scanf("%d",&c);
for(i=0;i
printf("\nThe Result is:\n");
for(i=0;i
printf("\n");
}
getch();
}
Syntactical errors may happened.
Why you cant initialise a value of a variable inside public in class in c plus plus?
I see no reason why you can't do that. The question must be mis stated. Please clarify, and show your code.
How do you install CCS V4.088?
Install CCS V4 then install the following updates one after the other:
pcbupd.exe
pcmupd.exe
pchupd.exe
pcwhupd.exe
pcdupd.exe
pcwupd.exe
setup_icd.exe
To print automorphic number in c plus plus language?
#include <stdio.h>
main (int argc, char **argv)
{
long inx, num, n, ns, r1, r2;
num = atol(argv[1]);
printf("num=%ld\n", num);
for (inx = 1; inx < num; inx++)
{
n = inx;
ns = n*n;
while (n > 0)
{
r1 = n%10;
r2 = ns%10;
if (r1 != r2) break;
n = n/10;
ns = ns/10;
}
if (n == 0)
{
printf("%ld is automorphic\n", inx);
}
}
}
A c plus plus program that defines vowels?
#include<locale>
#include<iostream>
#include<string>
bool is_vowel(const char c)
{
static const std::string vowels = "AEIOU";
return( vowels.find(toupper(c))<vowels.size() );
}
int main()
{
std::string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(size_t i=0; i<alphabet.size(); ++i)
{
std::cout<<'\''<<alphabet[i]<<"\' is ";
if( !is_vowel( alphabet[i] ))
std::cout<<"not ";
std::cout<<"a vowel."<<std::endl;
}
}
Output:
'a' is a vowel.
'b' is not a vowel.
'c' is not a vowel.
'd' is not a vowel.
'e' is a vowel.
'f' is not a vowel.
'g' is not a vowel.
'h' is not a vowel.
'i' is a vowel.
'j' is not a vowel.
'k' is not a vowel.
'l' is not a vowel.
'm' is not a vowel.
'n' is not a vowel.
'o' is a vowel.
'p' is not a vowel.
'q' is not a vowel.
'r' is not a vowel.
's' is not a vowel.
't' is not a vowel.
'u' is a vowel.
'v' is not a vowel.
'w' is not a vowel.
'x' is not a vowel.
'y' is not a vowel.
'z' is not a vowel.
'A' is a vowel.
'B' is not a vowel.
'C' is not a vowel.
'D' is not a vowel.
'E' is a vowel.
'F' is not a vowel.
'G' is not a vowel.
'H' is not a vowel.
'I' is a vowel.
'J' is not a vowel.
'K' is not a vowel.
'L' is not a vowel.
'M' is not a vowel.
'N' is not a vowel.
'O' is a vowel.
'P' is not a vowel.
'Q' is not a vowel.
'R' is not a vowel.
'S' is not a vowel.
'T' is not a vowel.
'U' is a vowel.
'V' is not a vowel.
'W' is not a vowel.
'X' is not a vowel.
'Y' is not a vowel.
'Z' is not a vowel.
How you work character type data in array c plus plus?
It's not clear from the question what you mean by "work". However character data types (char and wchar_t) are intended to store character codes and they work exactly the same whether as a single variable or as an array of characters. If you want to use the array as a string, however, remember to include a null-terminator at the end of the string.
How do you get Dark GDK Visual Studio C plus plus projects?
GDK tutorial websites are probably your best bet.
No, they are not the same. Describing yourself as a C programmer implies you have little or no experience with C++, which puts you at a distinct disadvantage over those who know both. Indeed, there are very few professional C++ programmers who are not also familiar with both C and assembler. Ideally, you should know all three and should have a clear understanding of how they are related.
With regards Polaris, one of the qualifications for the ISDP is "...a proven academic success in key Computer Science courses with an emphasis in Windows Environments." Thus it can be assumed that non-C++ programmers need not apply since C++ is a key computer science, particularly with regards Windows development.
What is the program that prints all prime numbers from 0 to 300 in C plus plus?
#include
bool is_prime(unsigned num)
{
// 2 is the first prime and the only even prime.
if (num < 2) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
// Divide num by every odd number from 3 up
// to and including the square root of num.
// If any division results in no remainder
// then num is not prime.
unsigned max = sqrt ((double) num) + 1;
unsigned div = 3;
do {
if (num % div == 0)
return false;
} while ((div += 2) < max);
// If we get this far, num is prime.
return true;
}
int main()
{
std::cout << "Prime numbers in the range 0 to 300:\n" << std::endl;
unsigned num = 0;
do {
if (is_prime (num))
std::cout << '\t' << num << std::endl;
} while( num++ < 300);
std::cout << std::endl;
}
Example Output
Prime numbers in the range 0 to 300:
2
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
211
223
227
229
233
239
241
251
257
263
269
271
277
281
283
293
What is data abstraction and data hiding concept in c plus plus?
Data abstraction refers to the way users can interact with class data without needing to know how that data is stored or manipulated by the class. For instance, a class method such as int GetValue() will obviously return an integer, but the value from which that integer comes need not be an integer (it could be a float) or it could be calculated on the fly based upon other internal data members. The point is that it is irrelevant to the user how the value was obtained.
Although many will tell you that data hiding (or information hiding) refers to the way in which classes "hide" private or protected information within themselves, that is not the case at all. What they are really referring to is data abstraction, as outlined above.
Note that private or protected access only limits access to the information, it does not physically hide it in any way, shape or form. That is, a private data member can be thought of as being placed in a virtual cage: your compiler can see the member exists, but unless your code is a member of the class then you cannot access it, and the compiler will tell you as much. The following example demonstrates the concept:
class a {
private: int data; };
class b: public a {
void foo { a::data = 5; }
}
In the above example, a::data is clearly visible and nothing can physically prevent you from writing the code shown above. Your compiler will obviously tell you that a::data is not accessible from b, but in order to do so the compiler must know that a::data exists. And if the compiler knows it exists, then so do you, even if the declaration appears in another file, thus the data cannot be said to be hidden.
The true meaning of data hiding is the way in which a binary executable or library hides information from the user. That is, the information is hidden through obfuscation. A skilled disassembler can still reveal the internal details so it is not completely hidden, but it would be difficult, if not impossible, even for a skilled disassembler, to reconstruct the original classes from which that data was produced, unless they have access to the declaration.
Writing statements in a class member function that use cout or cin is not recommended because it binds your class to the (global) standard IO streams, which is rarely a good idea. It goes against the OOP paradigm which encourages generalisation and where independent classes should express independent notions independently.
Class member functions should only be implemented when those functions operate solely upon the class itself, which primarily refers to class mutators (assignment operators and other "setters") as well as accessors ("getters"). If a class member function mutates a stream, then that function is really operating upon two classes simultaneously and should really be declared external to both, perhaps as a friend function if it also requires private access to the class representation.
With a well-designed class hierarchy, overloading the operator<< and operator>> operators usually suffices for most streaming requirements. If you require something more specialised, consider exposing a protected interface and allow users to provide their own implementations rather "hard wiring" your own.
csv flat file. I have this class now, you are welcome
Write a program to find the sum of squares of n natural numbers using c plus plus?
#include
#include
void main()
{
int sum_sqr(int n);
clrscr();
printf("%d",sum_sqr(5));//Sum of first 5 natural numbers' square values
getch();
}
int sum_sqr(int n)
{
int i,sum=0;
for(i=1;i<=n;i++)
{
sum=sum+(i*i);
}
return sum;
}
output:
55